GoogleColaboratoryでPythonを勉強している初学者です。
googleColab上のファイルにモジュールを作って、別ファイルで読み込むにはどうしたらよいですか?
以下、試したことです。
python
1def yen2dollar(yen, rate, charge = 0) : 2 dollar = yen / (rate + charge) 3 return dollar 4 5def dollar2yen(dollar, rate, charge = 0) : 6 yen = dollar * (rate - charge) 7 return yen
上記のコードをexchange.pyとしてgoogleColabのファイルに保存。
↓
同じフォルダの別ファイルでimport exchangeを実行。以下、エラー内容です。
python
1ImportErrorTraceback (most recent call last) 2<ipython-input-1-644049d1733b> in <module>() 3----> 1 import exchange 4 5ImportError: No module named exchange
別ファイルでexchangeモジュールを読み込み、中の関数を呼び出すにはどうしたらよいでしょうか。最終的には以下のモジュール(mymoney.py)を作り、実行できるようになることです。
python
1import exchange 2yen = 25000 3rate = 114.22 4charge = 1.0 5dollar = exchange.yen2dollar(yen, rate, charge) 6print(f"{dollar :,.2f}ドル")
※こちらが参考になるかなと思ったのですが具体的な方法がわかりません。どなたか教えてくださいm(. .)m
回答1件
あなたの回答
tips
プレビュー