tw0fifths
tw0fifths

Reputation: 37

possible global variable issue?

I am working on this homework assignment and I am stuck on what I know to be a very simple problem. The premise of the code is a petty cash system in which the user can make deposits, withdraws, and get their current balance. My problem is that the withdraws and deposits don't seem to be going through my Account class. Is it a global variable issue? I'll be adding timestamps later. Thank you in advance.

import datetime
import time

class Account:
    def __init__(self, initial):
        self.balance = initial

    def deposit(self, amt):
        self.balance = self.balance + amt

    def withdraw(self,amt):
        self.balance = self.balance - amt

    def getbalance(self):
        return self.balance

def yesno(prompt): 
    ans = raw_input(prompt) 
    return (ans[0]=='y' or ans[0]=='Y')

def run():  
    done = 0   
    while not done: 
        user_options()
        print
        done = not yesno("Do another? ")
        print

def user_options():
    print ("Here are your options:")
    print
    print ("   (a) Deposit cash")
    print ("   (b) Withdraw cash")
    print ("   (c) Print Balance")
    print
    u = raw_input("Please select a letter option: ").lower()
    user_input(u)

def user_input(choice):
    account = Account(0.00)
    if choice == "a" or choice == "b" or choice == "c":

        if choice == "a":
            d = input("Enter Deposit Amount: $")
            account.deposit(d)

        if choice == "b":
            w = input ("Enter Withdraw Amount: $")
            account.withdraw(w)

        if choice == "c":
            print ("Balance Amount: $"),
            print account.getbalance()

    else:
        print ("Not a correct option")


run()

#now = datetime.datetime.now()

Python Version: 2.7.2

Upvotes: 1

Views: 116

Answers (2)

John La Rooy
John La Rooy

Reputation: 304137

You don't need global variables, just restructure the code slightly like this

def run():  
    account = Account(0.00)
    done = 0   
    while not done: 
        user_option = user_options()
        user_input(user_option, account)

        print
        done = not yesno("Do another? ")
        print

def user_options():
    print ("Here are your options:")
    print
    print ("   (a) Deposit cash")
    print ("   (b) Withdraw cash")
    print ("   (c) Print Balance")
    print
    return raw_input("Please select a letter option: ").lower()

def user_input(choice, account):
    if choice == "a" or choice == "b" or choice == "c":

        if choice == "a":
            d = input("Enter Deposit Amount: $")
            account.deposit(d)

        if choice == "b":
            w = input ("Enter Withdraw Amount: $")
            account.withdraw(w)

        if choice == "c":
            print ("Balance Amount: $"),
            print account.getbalance()

    else:
        print ("Not a correct option")


run()

You can still do more to make the code nicer, this is just enough to get the account part working

Upvotes: 0

agf
agf

Reputation: 176740

The problem is:

account = Account(0.00)

Every time user_input is called, you create a new, empty account. Since user_input is called from user_options which is called inside the while loop in run, this happens before every transaction.

You need to move that line out of the loop, for example:

def run():
    done = 0
    global account # just showing you one way, not recommending this
    account = Account(0.00)
    while not done:
        user_options()
        print
        done = not yesno("Do another? ")
        print

Upvotes: 1

Related Questions