Keep a running total(budget) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Keep a running total(budget) (/thread-1147.html) |
Keep a running total(budget) - mcmxl22 - Dec-07-2016 I am trying to write a budget program. I need to figure out how to keep a running total. How do I update the variable bal via a user input? I have tried while and for loops. This is as close as I can get. bal = 0.00 def budget(): menu = ('1 Set Ballance', '2 Add Transaction', '3 Check Ballance') for m in menu: print '%s' % m what = raw_input('What do you want to do?\n> ').isdigit() print bal + what budget()
RE: Keep a running total(budget) - nilamo - Dec-07-2016 .isdigit() converts whatever you type into a bool True/False. So if you type 2, what will always be True. When adding a bool to a number, it's silently converted to "1", which is why you're seeing 1.0. Instead of what = raw_input("???").isdigit() , try:what = raw_input("???") if what.isdigit(): if what == "1": bal = float(raw_input("new balance: ")) elif what == "2": # etc... RE: Keep a running total(budget) - mcmxl22 - Dec-07-2016 OK, I totally fudged the code so here is an update: bal = 0.00 def budget(): menu = ('1 Set Ballance', '2 Add Transaction', '3 Check Ballance') for m in menu: print '%s' % m what = raw_input('What do you want to do?\n> ') if what in {'1'}: enter = input('Enter amount.\n> ') print enter + bal budget()How do I permanently change the value of bal ?
RE: Keep a running total(budget) - nilamo - Dec-07-2016 A single equal sign assigns values. enter = float(raw_input('enter amount')) bal = enter # bal is now permanently set |