Python计算器遇到无限循环

2024-06-01 04:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经挣扎了几个小时了,我不能完全围绕着这个想法。。。因此,当我运行这个程序时,它会立即进入while块的异常部分的无限循环“Must be a number value”。你知道吗

我唯一能想到的是它进入了一个无限循环,因为它没有读取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()

Tags: numbervaluemainbepaymentppltotaleach
2条回答

好像你有缩进问题,从这一行:

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("") 
  1. 从第44行到第68行缺少缩进
  2. 如果您使用的是python3,那么应该用input()替换原始的输入()(https://docs.python.org/3/whatsnew/3.0.html

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

相关问题 更多 >