Search code examples
pythonloopsprogram-entry-point

Python Calculator encountering infinite loops


I have been struggling with this for a few hours now and I can't quite wrap my mind around this... so when I run this it immediately goes into a infinite loop of "Must be a number value" from the exception part of the while block.

The only thing I can think of is It's going into an infinite loop because its not reading the main(), or my logic is just completely wrong. Why would it be reading one string from within a structure where nothing seems to exist.. the question " How much is the bill?" never even appears(This should be the 1st thing the user sees).. it just goes right into the loop.

I know it must be something really silly that I am missing, but I can't seem to locate why the code is behaving how it is.

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

Solution

    1. there is a missing indention from line 44 until line 68
    2. if you're using python 3 you should replace raw_input() with input() (https://docs.python.org/3/whatsnew/3.0.html)

    working Python 3 version:

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