Python计算器遇到无限循环



我已经为此挣扎了几个小时了,我不能完全把我的头脑围绕这个…所以当我运行这个时,它立即进入一个无限循环,从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()
  1. 从第44行到第68行缺少缩进
  2. 如果你使用python 3,你应该用input() (https://docs.python.org/3/whatsnew/3.0.html)代替raw_input()

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()

似乎你有一个缩进的问题,从行:

print ("Tip Percentage?")

直到:

if __name__ == '__main'__:

代码需要有更多的缩进,这样它就会成为main的一部分。

此外,最好捕获异常并打印其消息,以便您可以轻松找到导致异常的原因并修复它。请修改:

except:
        print("")
        print("Must be a number value")
        print("") 

:

except Exception, e:
        print("")
        print("Must be a number value (err: %s)" % e)
        print("") 

相关内容

  • 没有找到相关文章

最新更新