前提
python
1# 関数introduceを作ってください 2def introduce (n, age): 3 print(n + " is my name. I'm " + str(age) + " years old.") 4# 関数の呼び出し 5introduce("Yamada", 18)
というコードを発展させて、Yamada以外に、人数が増えても動くコードを作ろうとしています。
実現したいこと
次のような出力結果を得ることを望んでいます。
Yamada is my name. I'm 18 years old.
Tanaka is my name. I'm 20 years old.
Sato is my name. I'm 17 years old.
試したこと
2つのコードを試しました。
python
1# 関数introduceを作ってください 2def introduce (n, age): 3 print(n + " is my name. I'm " + str(age) + " years old.") 4# 関数の呼び出し 5introduce(["Yamada", 18],["Tanaka", 20],["Sato", 17])
結果はTypeError: introduce() takes 2 positional arguments but 3 were givenでした。
次に辞書型を作れば良いのかもと思って、次のコードを書きました。
python
1# 関数introduceを作ってください 2def introduce (n, age): 3 print(n + " is my name. I'm " + str(age) + " years old.") 4# 関数の呼び出し 5introduce({"Yamada": 18}, 6 {"Tanaka": 20}, 7 {"Sato" : 17})
結果はTypeError: unsupported operand type(s) for +: 'dict' and 'str'でした。
どのようなコードがいいのでしょうか。お助け願います。
補足情報(FW/ツールのバージョンなど)
Google Colabを使っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/15 09:23
2022/09/15 09:42
2022/09/15 14:13 編集
2022/09/16 05:21 編集
2022/09/16 08:04