嘿傢伙即時新的和我即將開始。 我試圖通過簡單地獲得輸入和計算來製造BMI計算器,但由於某種原因它不起作用。 這是程序:通過類型爲'str'的非int的乘法序列
print "how old are you?",
age = raw_input()
print "how tall are you?",
height = raw_input()
print "how much do you weigh?",
weight = raw_input()
print "So, you're %r years old, %r meters tall and %r Kilograms heavy.\n Ain't too bad but could be better to be honest!" %(age, height, weight)
print "Your BMI is %d" % (weight (height * height))
,這是輸出:
how old are you? 1
how tall are you? 2
how much do you weigh? 4
So, you're '1' years old, '2' meters tall and '4' Kilograms heavy.
Ain't too bad but could be better to be honest!
Traceback (most recent call last):
File "ex10.py", line 11, in <module>
print "Your BMI is %d" % (weight (height * height))
TypeError: can't multiply sequence by non-int of type 'str'
謝謝你們!
我不知道爲什麼它不會像我的代碼中分隔線... –
[如何將字符串轉換爲Python中的整數?](http://stackoverflow.com/questions/ 642154/how-to-convert-strings-into-in-in-python) –