这是我的一个学校项目代码(我正在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'
此外,如果我在询问货币类型时输入数字值,则程序将退出而不显示任何消息。 这是一个学校项目,因此我希望从用户那里得到错误的输入,并需要向他们提供所需的错误消息,同时试图使他们返回并输入正确的消息。
答案 0 :(得分:1)
如评论中所述,mon
和raw
隐式为strings
。
要转换它们,请使用mon = int(mon)
或mon = float(mon)
。
请小心,因为您应该注意输入无效(尝试-TypeError
块除外)。
一些提示:
-最后,如果应该使用and
而不是or
-这两行之间没有区别:
if cur == 'Eur' or cur == 'Eur':
print (euconverter(mon, rate))
elif cur == 'GBP' or cur == 'Gbp':
print (euconverter(mon, rate))
如果有意的话,您可以将它们分组在一起
答案 1 :(得分:1)
似乎您要尝试的是提示用户输入并验证输入,然后再次询问输入是否无效。
您可以通过将每个raw_input()
包裹在带有标志的while
循环中来进行尝试。
要验证货币,将输入转换为大写然后再次检查允许的可能性列表将更容易。
要验证金额和费率,可以将它们强制转换为try-except块内的float
。
valid_currencies = ['EUR', 'GBP']
cur = None
mon = None
rate = None
is_valid = False
while not is_valid:
cur = raw_input('Please give me the currency').upper()
if cur in valid_currencies:
is_valid = True
else:
print 'Not a valid currency'
is_valid = False
while not is_valid:
try:
mon = float(raw_input('Please give me the value'))
is_valid = True
except ValueError:
print 'Not a valid value'
is_valid = False
while not is_valid:
try:
rate = float(raw_input('Please give me the rate'))
is_valid = True
except ValueError:
print 'Not a valid rate'
print 'Converted amount'
print mon * rate
答案 2 :(得分:0)
一些用于文本输入的重构代码如下:
legal_input = ["gbp", "eur", "yen"]
currency = raw_input("Please enter the currency type")
if currency.lower() in legal_input:
#some code to do the required operations
else:
print('not a recognised currency type!')
答案 3 :(得分:0)
def euconverter(mon, rate):
return mon * rate
cur = raw_input('Please give me the currency type ')
mon = float(raw_input('Please give me the ammount of money '))
rate = float(raw_input('Please give me the rate of the exchange '))
while True:
#i dont know how to make the false statement ending the program as well not necessary i think
if cur == 'EUR' or cur == 'Eur' or cur == 'GBP' or cur == 'Gbp':
if cur == 'EUR' or cur == 'Eur':
print (euconverter(mon, rate))
break
elif cur == 'GBP' or cur == 'Gbp':
print (euconverter(mon, rate))
break
# I dont know if multiple breaks should be included here
else:
print 'wrong input'
cur = raw_input('Please give me the currency type again correcntly this time ')
continue
有了大家的提示和帮助,我终于开始运行它,提示用户在错误的货币输入行中输入其他货币,感谢您的帮助,您很快就会再次收到我的来信。