Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Arbitrary Keyword Arguments in Python | Section
Python Functions
セクション 1.  13
single

single

bookArbitrary Keyword Arguments in Python

メニューを表示するにはスワイプしてください

In programming, there is a special syntax for passing any number of named parameters to a function, known as **kwargs.

**kwargs allows a function to accept any number of named arguments and treat them as a dictionary.

123456
def example_function(**kwargs): for key, value in kwargs.items(): print(f'{key}: {value}') # Example function call example_function(name='John', age=25, city='New York')
copy

In this example, **kwargs receives named arguments and prints their keys and values.

Note
Note

The .items() method is used to obtain a list of key-value pairs from a dictionary in Python. Each element in this list is represented as a tuple (key, value).

タスク

スワイプしてコーディングを開始

Implement a function that filters products based on a given budget. The function should return a list of affordable products or indicate if no products are available within the budget.

  • The function filter_products_by_budget takes a budget and any number of named product–price pairs using **kwargs.
  • Inside the loop, use kwargs.items() to get each product name and its price.
  • Compare the values and check if budget is greater than or equal to price.
  • When the condition is true, store the product and its price in the affordable_products dictionary.
  • If the dictionary stays empty, return "No products available within the budget.".
  • If at least one product is added, return "Available products within budget: {affordable_products}".

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  13
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt