【发布时间】:2019-04-12 20:07:43
【问题描述】:
这是我的一个学校项目的代码(我写在Python 2.7.13):
def euconverter(mon, rate):
return (mon * rate)
cur = raw_input('Please give me the currency')
mon = raw_input('Please give me the value')
rate = raw_input('Please give me the rate')
while cur == 'EUR' or cur == 'Eur' or cur == 'GBP' or cur == 'Gbp':
if cur == 'Eur' or cur == 'Eur':
print (euconverter(mon, rate))
elif cur == 'GBP' or cur == 'Gbp':
print (euconverter(mon, rate))
else:
if cur != 'EUR' or cur != 'Eur' or cur != 'GBP' or cur != 'Gbp':
print 'Wrong input'
break
我收到此错误:
Traceback (most recent call last):
File "C:/Users/Maple/PycharmProjects/untitled/Mid term Project.py", line 15, in <module>
print (euconverter(mon, rate))
File "C:/Users/Maple/PycharmProjects/untitled/Mid term Project.py", line 2, in euconverter
return int(mon * rate)
TypeError: can't multiply sequence by non-int of type 'str'
另外,如果我在询问货币类型时输入了一个数值,那么程序会退出而不显示任何消息。 这是一个学校项目,所以我希望从用户那里得到错误的输入,并且需要向他们提供所需的错误消息,同时试图让他们返回并输入正确的错误消息。
【问题讨论】:
-
mon和rate是字符串。您需要将它们转换为数字。 -
您的
while也是一个无限循环,因为您没有在其中读取cur的新值。 -
@khelwood 部分答案是相同的,是的,但问题还询问有关捕获输入错误的问题
-
@ScottAnderson 是的,但我也不能投票给“太宽泛”。
标签: python python-2.7