How To Update Variable In Python

Viewed 788

how do I update a balance of the variable constanta

def menu():
   balance = float(100.0)
   print (" 1.Balance 2.Draw ")
   ask = ('choose')

   if (ask == '1'):
       print ("Your Balance is {0}".format, balance)
       ask2 = input('do you want go back to menu ?')

       if (ask2 == 'y'):
          menu()
       else:
          sys.exit()

   elif (ask == '2'):
       draw = input("How Much : ")
       new_balance = (balance - draw)
       balance = new_balance
       print (balance)
       ask2 = input('do you want go back to menu ?')

       if (ask2 == 'y'):
          menu()
       else:
          sys.exit()

So when my first input is 1 the output is 100.0, then I go back to menu and the second input I choose 2 with the draw input of 50 so the new_balance is 50. I then go back to menu() and choose input 1 but the balance variable is still 100.0.

How To Update Variable Until balance = 0

1 Answers

Because you are assignin 100.0 to balance variable whenever you entered menu function. For solving this problem, you can create a balance global variable or you can use while loop.

 def menu():
        balance = float(100.0)
        while true:
            print (" 1.Balance 2.Draw ")
            ask = input('choose')

            if (ask == '1'):
                print ("Your Balance is {0}".format, balance)
                ask2 = input("do you want go back to menu ?")

                if (ask2 == 'y'):
                    continue
                else:
                    break

            elif (ask == '2'):
                draw = input("How Much : ")
                balance = (balance - draw)
                print (balance)
                if(balance == 0):
                    print("Your balance is 0")
                    break
                ask2 = input("do you want go back to menu ?")

                if (ask2 == 'y'):
                    continue
                else:
                    break
Related