我几个小时以来一直在努力解决这个问题,我无法完全理解这一点......所以当我运行它时,它会立即进入无限循环的“#34;必须是一个数值"来自while块的异常部分。
我唯一能想到的是它进入无限循环,因为它没有读取main(),或者我的逻辑完全错误。为什么它会从一个似乎不存在的结构中读取一个字符串。问题"账单多少钱?"从来没有出现过(这应该是用户看到的第一件事)..它正好进入循环。
我知道我一定很傻,但我似乎无法找到代码行为的原因。
# what each person pays, catch errors
def payments(bill,ppl):
try:
return round((bill/ppl),2)
except:
print ('Invalid Calculation, try again')
#function to calculate tip, catch any errors dealing with percentages
def tip(bill,ppl,perc):
try:
return round(((bill * (perc/100))/ppl),2)
except:
print ('Please retry calculation with valid tip percentage')
'''
function of body that will
ask each question and will catch errors(if any),
and continue to loop until valid entry is given
'''
def main():
print ("How much is the bill?")
while True:
try:
total_bill = float(raw_input('>> $'))
break
except:
print("")
print("Must be a number value")
print("")
print("")
print ("How many people?")
while True:
try:
num_ppl = int(raw_input('>>'))
break
except:
print("")
print("Must be a number value")
print("")
print("")
print ("Tip Percentage?")
while True:
try:
perc = int(raw_input('>> %'))
break
except:
print("")
print("Must be a number value")
print("")
print ("")
print ("Calculating Payment...")
# Create variables to calculate total pay
bill_payment = payments(total_bill,num_ppl)
tip_payment = tip(total_bill,perc,num_ppl)
total_payment = float(bill_payment)+float(tip_payment)
#print each variable out with totals for each variable
print ('Each Person pays $%s for the bill' % \
str(bill_payment))
print ('Each Person pays $%s for the tip' % \
str(tip_payment))
print ('Which means each person will pay a total of $%s' % \
str(total_payment))
if __name__ == '__main__':
main()
答案 0 :(得分:2)
使用Python 3版本:
# what each person pays, catch errors
def payments(bill,ppl):
try:
return round((bill/ppl),2)
except:
print ('Invalid Calculation, try again')
#function to calculate tip, catch any errors dealing with percentages
def tip(bill,ppl,perc):
try:
return round(((bill * (perc/100))/ppl),2)
except:
print ('Please retry calculation with valid tip percentage')
'''
function of body that will
ask each question and will catch errors(if any),
and continue to loop until valid entry is given
'''
def main():
print ("How much is the bill?")
while True:
try:
total_bill = float(input('>> $'))
break
except:
print("")
print("Must be a number value")
print("")
print("")
print ("How many people?")
while True:
try:
num_ppl = int(input('>>'))
break
except:
print("")
print("Must be a number value")
print("")
print("")
print ("Tip Percentage?")
while True:
try:
perc = int(input('>> %'))
break
except:
print("")
print("Must be a number value")
print("")
print ("")
print ("Calculating Payment...")
# Create variables to calculate total pay
bill_payment = payments(total_bill,num_ppl)
tip_payment = tip(total_bill,perc,num_ppl)
total_payment = float(bill_payment)+float(tip_payment)
#print each variable out with totals for each variable
print ('Each Person pays $%s for the bill' % \
str(bill_payment))
print ('Each Person pays $%s for the tip' % \
str(tip_payment))
print ('Which means each person will pay a total of $%s' % \
str(total_payment))
if __name__ == '__main__':
main()
答案 1 :(得分:1)
似乎你有一个缩进问题,从行:
print ("Tip Percentage?")
直到行:
if __name__ == '__main'__:
代码需要更多缩进,因此它将成为主要代码的一部分。
此外,最好捕获异常并打印其消息,以便您可以轻松找到导致异常的原因并进行修复, 请改变这个:
except:
print("")
print("Must be a number value")
print("")
对此:
except Exception, e:
print("")
print("Must be a number value (err: %s)" % e)
print("")