我已經堅持了兩個星期,可能是一個非常基本和簡單的問題。我想創建一個非常簡單的程序(例如,我正在開發一個BMI計算器),以便使用一個模塊。我寫了它,我仍然不明白爲什麼它不起作用。我修改了很多次,試圖找到解決辦法,所以我有很多不同的錯誤信息,但在這個版本我的程序中,該消息爲(它要求進入高度後):如何使用我在python中創建的簡單模塊?
Enter you height (in inches): 70
Traceback (most recent call last):
File "C:/Users/Julien/Desktop/Modules/Module ex2/M02 ex2.py", line 6, in <module>
from modBmi import *
File "C:/Users/Julien/Desktop/Modules/Module ex2\modBmi.py", line 11, in <module>
modBmi()
File "C:/Users/Julien/Desktop/Modules/Module ex2\modBmi.py", line 5, in modBmi
heightSq = (height)**2
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'"
這是我的代碼(信息,我的模塊是一個分離的文件「modBmi.py」,但比我的主程序相同的文件夾):
#Python 3.4.3
#BMI calculator
def modBmi():
#ask the height
height = input ("Enter you height (in inches): ")
#create variable height2
heightSq = int(height)**2
#ask th weight
weight = input ("Enter you weight (in pounds): ")
#calculate bmi
bmi = int(weight) * 703/int(heighSq)
modBmi()
#import all informatio from modBmi
from modBmi import *
#diplay the result of the calculated BMI
print("Your BMI is: " +(bmi))
謝謝它幫助我走得更遠!儘管如此,它還是說我在進入高度和體重之後測試程序時沒有定義(在我的主程序中)bmi。 – Pak
查看我的更新回答 – DavidG