Python Calculator encountering infinite loops -
i have been struggling few hours , can't quite wrap mind around this... when run goes infinite loop of "must number value" exception part of while block.
the thing can think of it's going infinite loop because not reading main(), or logic wrong. why reading 1 string within structure nothing seems exist.. question " how bill?" never appears(this should 1st thing user sees).. goes right loop.
i know must silly missing, can't seem locate why code behaving how is.
# each person pays, catch errors def payments(bill,ppl): try: return round((bill/ppl),2) except: print ('invalid calculation, try again') #function calculate tip, catch errors dealing percentages def tip(bill,ppl,perc): try: return round(((bill * (perc/100))/ppl),2) except: print ('please retry calculation valid tip percentage') ''' function of body ask each question , catch errors(if any), , continue loop until valid entry given ''' def main(): print ("how bill?") while true: try: total_bill = float(raw_input('>> $')) break except: print("") print("must number value") print("") print("") print ("how many people?") while true: try: num_ppl = int(raw_input('>>')) break except: print("") print("must number value") print("") print("") print ("tip percentage?") while true: try: perc = int(raw_input('>> %')) break except: print("") print("must number value") print("") print ("") print ("calculating payment...") # create variables 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 totals each variable print ('each person pays $%s bill' % \ str(bill_payment)) print ('each person pays $%s tip' % \ str(tip_payment)) print ('which means each person pay total of $%s' % \ str(total_payment)) if __name__ == '__main__': main()
- there missing indention line 44 until line 68
- if you're using python 3 should replace raw_input() input() (https://docs.python.org/3/whatsnew/3.0.html)
working python 3 version:
# each person pays, catch errors def payments(bill,ppl): try: return round((bill/ppl),2) except: print ('invalid calculation, try again') #function calculate tip, catch errors dealing percentages def tip(bill,ppl,perc): try: return round(((bill * (perc/100))/ppl),2) except: print ('please retry calculation valid tip percentage') ''' function of body ask each question , catch errors(if any), , continue loop until valid entry given ''' def main(): print ("how bill?") while true: try: total_bill = float(input('>> $')) break except: print("") print("must number value") print("") print("") print ("how many people?") while true: try: num_ppl = int(input('>>')) break except: print("") print("must number value") print("") print("") print ("tip percentage?") while true: try: perc = int(input('>> %')) break except: print("") print("must number value") print("") print ("") print ("calculating payment...") # create variables 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 totals each variable print ('each person pays $%s bill' % \ str(bill_payment)) print ('each person pays $%s tip' % \ str(tip_payment)) print ('which means each person pay total of $%s' % \ str(total_payment)) if __name__ == '__main__': main()
Comments
Post a Comment