1

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
  • Is there a reason you do a recursive function instead of a simple while loop? Commented Oct 27, 2019 at 11:39

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.