0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プログラミング習得ログ デフォルト引数

Posted at

演習問題はこちら

def convert_temperature_data(temp_data, target_unit="celsius"):
    """
    温度データを変換する関数
    引数: temp_data = [("東京", 25, "celsius"), ("New York", 77, "fahrenheit"), ...]
          target_unit = "celsius" または "fahrenheit"
    戻り値: 変換後のデータのリスト
    """
    # 摂氏 ⇔ 華氏変換
    # 摂氏から華氏: F = C × 9/5 + 32
    # 華氏から摂氏: C = (F - 32) × 5/9
    # ここにコードを書いてください
    pass

# 期待する動作
data = [("東京", 25, "celsius"), ("New York", 77, "fahrenheit"), ("London", 15, "celsius")]

celsius_data = convert_temperature_data(data, "celsius")
print(celsius_data)
# [("東京", 25.0, "celsius"), ("New York", 25.0, "celsius"), ("London", 15.0, "celsius")]

fahrenheit_data = convert_temperature_data(data, "fahrenheit")
print(fahrenheit_data)
# [("東京", 77.0, "fahrenheit"), ("New York", 77.0, "fahrenheit"), ("London", 59.0, "fahrenheit")]

何度かつまづきながらも、自分で書けた。もっとシンプルにできそう。

def convert_temperature_data(temp_data, target_unit="celsius"):
    """
    温度データを変換する関数
    引数: temp_data = [("東京", 25, "celsius"), ("New York", 77, "fahrenheit"), ...]
          target_unit = "celsius" または "fahrenheit"
    戻り値: 変換後のデータのリスト
    """
    # 摂氏 ⇔ 華氏変換
    # 摂氏から華氏: F = C × 9/5 + 32
    # 華氏から摂氏: C = (F - 32) × 5/9
    processed_data=[]
    for city,number,temperature in temp_data:
        if target_unit=='celsius':
            if temperature=='fahrenheit':
                number = (number-32)*5/9
                temperature = 'celsius'
        if target_unit=='fahrenheit':
            if temperature=='celsius':
                number = number*9/5+32
                temperature = 'fahrenheit'
        processed_data.append([city,number,temperature])
    return processed_data
    

# 期待する動作
data = [("東京", 25, "celsius"), ("New York", 77, "fahrenheit"), ("London", 15, "celsius")]

celsius_data = convert_temperature_data(data, "celsius")
print(celsius_data)
# [("東京", 25.0, "celsius"), ("New York", 25.0, "celsius"), ("London", 15.0, "celsius")]

fahrenheit_data = convert_temperature_data(data, "fahrenheit")
print(fahrenheit_data)
# [("東京", 77.0, "fahrenheit"), ("New York", 77.0, "fahrenheit"), ("London", 59.0, "fahrenheit")]

躓いたポイントなどをメモ

append()メソッドは1つの引数しか受け取れないのに、3つの引数を渡そうとした
processed_data = []
processed_data.append(city, temp, unit)  # これがエラーの原因

+append()は必ず1つの引数だけを受け取ります
+複数の値をまとめて追加したい場合は、タプル () やリスト [] で囲んで1つの要素にする
+あなたの温度変換の例では、processed_data.append((city, temp, unit)) と括弧でタプルにするのが正解です

デフォルト引数

以下の記述のtarget_unit="celsius"の部分のデフォルト引き数がいまいち理解出来ていなかった

def convert_temperature_data(temp_data, target_unit="celsius"):

temp_data の値とは無関係
target_unit="celsius" は:
❌ temp_data 内のタプルの値を見て決められているわけではない
❌ temp_data の最初の要素や最後の要素の単位を参照しているわけではない
✅ 単純に プログラマーが「摂氏がよく使われるだろう」と予想して設定した固定値

デフォルト引数の例

# 関数定義
def greet(name, greeting="こんにちは"):
    return f"{greeting}{name}さん!"

# 使用例
print(greet("田中"))                    # "こんにちは、田中さん!"
print(greet("田中", "おはよう"))         # "おはよう、田中さん!"
print(greet("田中", greeting="お疲れ様")) # "お疲れ様、田中さん!"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?