Gamecommunity =GCHQ=
http://forum.gamecommunity.co.uk:8080/

Python
http://forum.gamecommunity.co.uk:8080/viewtopic.php?f=6&t=48699
Page 1 of 1

Author:  Seric=GCHQ= [ Sun Jan 06, 2008 6:23 pm ]
Post subject:  Python

As part of my coursework, I have to create a couple of functions which act as a cash machine.

i.e.

withdraw/deposit from a balance.

heres what I have at the moment.

Image

Now, I know this doesnt work as it cant call on the information from the seperate functions etc. I would have stuck these into a single function, but this is how I set it out in order to show what I mean.

Is there a way I can get information from a previous function into a current one?

Whats the best/simplest way to go about this?

Thanks,

Seric

Author:  Chips=GCHQ= [ Sun Jan 06, 2008 6:48 pm ]
Post subject:  Re: Python

Are you talking about the value of balance? global balance

inside each function, put global balance as your first line, that way instead of treating balance as a local variable, it'll use the global variable instead.

Define balance with a value before you start calling the functions though ;)

(i would explain better with typed code, but the forum software thinks I'm trying injection, or xss or SOMETHING completely baloney and lame).

p.s shouldn't this be in geek section? :D

Author:  Seric=GCHQ= [ Mon Jan 07, 2008 1:43 am ]
Post subject:  Re: Python

simply putting balance = 0 infront of the whole thing won't work.

or is that not what you're suggesting?

Author:  Chips=GCHQ= [ Mon Jan 07, 2008 1:51 am ]
Post subject:  Re: Python

Right, that code won't do anything unless you call it :D

So you've got a call somewhere. Now what i propose is that you set balance as a global variable...

so balance = 0 as a line BEFORE those functions are called.

so after function definitions, you may have a few calls to them:
balance = 0
deposit(50)
withdraw(10)

Now the scope of balance will be a local variable within each of the functions by default. However, you can actually refer to the global variable balance (as seen initialised to 0 above) by putting in:
global balance
as the first line in each function..

so def funname(account)
global balance
balance = balance + account


the global balance part will ensure that every reference to balance within the function is referring to the global variable for balance.
Ergo, when it alters teh value of balance within the function, it's changing the global variable. When it, or another, function is called - it'll also use the global vairable, and therefore have the value it's been assigned.

Of course, if the problem wasn't accessing the balance.... then I'm not quite sure where you're having trouble :P

Code:

def deposit(amount):
   global bal
   bal = bal + amount
   print "balance is ", bal

def widthdraw(amount):
   global bal
   bal = bal - amount
   print "balance is ", bal

def balance():
   global bal
   print "balance is ", bal

bal = 0
deposit(50)
withdraw(10)
deposit(40)



That'd end up printing out:
balance is 50
balance is 40
balance is 80

However, don't have pyth on my system, so can't verify it - I may be talking bollocks :(
(typing out p*ython results in error?)

Author:  Seric=GCHQ= [ Mon Jan 07, 2008 4:16 am ]
Post subject:  Re: Python

sorted - thanks :D

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/