print "Welcome to my converter!"
#inches to centimeters
def in_inches(n):
return(n * 2.54)
#pounds to kilograms
def in_pounds(n):
return(n * 0.453592)
while True:
print "Which conversion?"
print "1 is for in to cm"
print "2 is for lbs to kg"
choice = raw_input("?")
if choice == "1":
n = raw_input("How many inches?")
res = in_inches(n)
print "In %s in we have %s cm." % (n, res)
elif choice == "2":
n = raw_input("How many pounds?")
res = in_pounds(n)
print str(n) + " is " + str(res) + " Kilograms"
else:
print "Invalid choice"
我有这个代码,但是当我将它引入控制台时它无法工作。控制台说:
Traceback (most recent call last):
File "converter.py", line 20, in <module>
res = in_inches(n)
File "converter.py", line 4, in in_inches
return(n * 2.54)
TypeError: can't multiply sequence by non-int of type 'float'
这是什么意思,我该如何解决? 提前致谢。 `
答案 0 :(得分:0)
n
的类型为字符串,您需要将其转换为整数,如下所示:
n = int(raw_input("How many inches?"))
和
n = int(raw_input("How many pounds?"))
raw_input()
会返回一个字符串,这就是您需要使用int()
将n
转换为int
的原因。