简体   繁体   中英

Python returning and passing variables

I'm trying to create a program to:

  1. Randomly generate an account balance;
  2. Assess whether that balance meets the minimum balance requirement of 10;
  3. if it does, print a string stating “no minimum balance fees apply”, or;
  4. if it does not, assess a fine of 10 and print the net balance after the fine.

Here is my code:

from random import randint

class calculate(object):

    def __init__(self, account_balance):
        self.account_balance = account_balance

    def applycharge(self):
        account_balance = self.account_balance
        if account_balance < 10:
            print "Minimum balance charges of $10 apply."
            account_balance = account_balance - 10
            print account_balance
            return account_balance
       else:
            print "No minimum balance charges apply."

    def printbalance(self):
        account_balance = self.account_balance
        print "The net account balance is: %d" % account_balance

initial_balance = randint(-100, 100)
print "The account balance before charges is: %d" % initial_balance


getcharge = calculate(initial_balance)
getcharge.applycharge()
getcharge.account_balance()

When I run it, here is an example of what I get:

The account balance before charges is: -74    
Minimum balance charges of $10 apply.    
-84    
The net account balance is: -74

So, everything is going ok until the last line. (And everything is also OK when the randomly-generated balance is not lower than 10, which causes the fine to be assessed.)

Note - I only I put the print statement in that displays the next-to-last line ( -84 ) to check that everything is fine up to that point.

The problem comes in when I want to display the “net account balance”. I am obviously displaying the original balance, before the fine of 10 is assessed.

I assume there is a problem in how I am returning the balance after the fine and passing it to the “printbalance” method. Or not passing it, as appears to be the case. Alternatively, this may have something to do with using the original, global variable when “printbalance” is invoked, rather than what I am trying to do, which is change that value, return it and then display it.

My whole point in writing this little piece of code was to exercise my brain in passing variables around and try to flesh out what I do not understand. Mission accomplished.

I'm going to stop there, because my explanation is about to become as tangled a spaghetti-bowl of gibberish as my brain is right now, but I hope this gives enough info to make it clear what I am trying to accomplish.

I did look at other similarly-worded questions/answers but I could not seem to get my answer out of them.

Finally, I'm sure there are all kinds of things wrong here - I've only been programming (or trying to) for about three weeks.

You only return the new balance, but then ignore the returned value, nor did you set the new value on your instance.

As such, the account_balance value you return is simply lost, ignored.

You probably wanted to update the attribute , so that getcharge.printbalance() will print the updated value:

def applycharge(self):
    account_balance = self.account_balance
    if account_balance < 10:
        print "Minimum balance charges of $10 apply."
        self.account_balance = account_balance - 10
   else:
        print "No minimum balance charges apply."

I removed the return line; the getcharge instance of your calculate class is responsible for tracking the account balance value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM