【发布时间】:2013-03-23 22:51:03
【问题描述】:
虽然我输入了一个数值,但它仍然给我一个错误。我不知道为什么会这样.. 帮助别人?
def is_string(s):
rate = input(s)
try:
str.isalpha(rate)
print('There was an error. Please try again. Make sure you use numerical values and alpabetical. ')
return is_string(s) #ask for input again
except:
return rate
ExRate = is_string('Please enter the exchange rate in the order of, 1 '+Currency1+' = '+Currency2)
def is_string2(msg):
amount = input(msg)
try:
str.isalpha(amount)
print('There was an error. Please try again. Make sure you use numerical values. ')
return is_string2(msg) #ask for input again
except:
return amount
Amount = is_string2('Please enter the amount you would like to convert:')
【问题讨论】:
-
你想在这里做什么?
-
阅读
isalpha()的文档,您可能会发现错误。 -
您希望
try子句的哪一部分引发错误?打印“有一个错误”不足以打破try语句。 -
@Ben OP 甚至没有这样做,因为他丢弃了结果并期望它无缘无故地抛出异常,因为这不是方法的作用,也没有理智的理由期待它会。
-
@Jemini 然后要做的事情是
try: amount = int(raw_input(...))如果有的话。我也会特别关注ValueError。
标签: python python-3.x function