Python Forum
Keep a running total(budget)
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
Reply
#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...
Reply
#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
Reply
#4
A single equal sign assigns values.
enter = float(raw_input('enter amount'))
bal = enter # bal is now permanently set
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Budget optimization in python ronjeremiah 0 1,685 May-16-2021, 01:18 PM
Last Post: ronjeremiah
  Running total counter Crenshaw 2 2,161 Mar-05-2021, 04:19 PM
Last Post: Larz60+
  Please help me to create company budget mad_accountant 2 2,032 Nov-21-2019, 06:01 PM
Last Post: mad_accountant

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020