Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Keep a running total(budget)
#1
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.  Wall

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()
Output:
1 Set Ballance 2 Add Transaction 3 Check Ballance What do you want to do? > 1 1.0
Quote
#2
.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...
Quote
#3
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 ?

Output:
1 Set Ballance 2 Add Transaction 3 Check Ballance What do you want to do? > 1 Enter amount. > 200 200.0
Quote
#4
A single equal sign assigns values.
enter = float(raw_input('enter amount'))
bal = enter # bal is now permanently set
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Please help me to create company budget mad_accountant 2 162 Nov-21-2019, 06:01 PM
Last Post: mad_accountant

Forum Jump:


Users browsing this thread: 1 Guest(s)