python - Extra lines being printed based on earlier input datatype -


my question how program handles unexpected inputs. seems work fine except when string entered.

when 1 inputs string coin, message "please enter valid input" appears , main run again, expected. when 1 of amounts in coins entered correctly, program spits out proper input, each time incorrect string entered earlier, seems 'remember' , prints "thank patronage, paid: 0 cad". example, following terminal session occur:

please select coin value in pennies (no toonies allowed): asd
please enter valid input
please select coin value in pennies (no toonies allowed): hmm
please enter valid input
please select coin value in pennies (no toonies allowed): 100
thank patronage, paid: 100 c cad
thank patronage, paid: 0 c cad
thank patronage, paid: 0 c cad

i want statement regarding 100c show up. i don't understand causing code 'remember' string inputs. code below, stumped why happening. when else statement in coinamount() ran, shouldn't program 'restart' if had run code, since user input being reassigned new prompt?

coins = [0, 5, 10, 25, 100]  def coinamount(total):     coin = (input("please select coin value in pennies (no toonies   allowed): "))      if coin == "q":         print("quitting...")      elif coin.isnumeric():         coin = int(coin)         total += coin      else:         print("please enter valid input")         main()      return total   def verification(coin):     if coin in coins:         print("thank patronage, paid:", coin, "c cad")      elif coin == 200:         print("i told don't accept toonies...")         main()      else:         print("please enter valid coin amount")         main()   def main():     tab = 0     tabplus = coinamount(tab)     verification(tabplus)  main() 

you confusing loops , recursion. when program runs right doing this:

main()  |  ---> coinamount()         |         ---> main()               |               ---> coinamount()                      |                      ---> ... 

that's not want in program. it's little bit unclear program should doing, imagine it's more main calls coinamount , coinamount loops until gets valid input , returns valid input main.

if want main keep running until user presses q, add loop main well.

one possible implementation below:

coins = [0, 5, 10, 25, 100]  def coinamount(total):     while true:         coin = (input("please select coin value in pennies (no toonies allowed): "))         if coin == "q":             print("quitting...")             return coin         elif coin.isnumeric():             coin = int(coin)             total += coin             return total         else:             print("please enter valid input")  def verification(coin):     if coin in coins:         print("thank patronage, paid:", coin, "c cad")     elif coin == 200:         print("i told don't accept toonies...")     else:         print("please enter valid coin amount")   def main():     tabplus = coinamount(0)     while tabplus != "q":         verification(tabplus)         tabplus = coinamount(tabplus)  main() 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -