Login    Forum    Register    Search    FAQ

Board index » HELP AND ADVICE » GAME/SOFTWARE/HARDWARE PROBLEMS




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Python
 Post Posted: Sun Jan 06, 2008 6:23 pm 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
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

_________________
Image
Image


Top 
 Profile  
 
 Post subject: Re: Python
 Post Posted: Sun Jan 06, 2008 6:48 pm 
Offline
I'm ghey 4 teh Hoff!
User avatar

Joined: Wed Sep 21, 2005 5:18 pm
Posts: 4142
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


Top 
 Profile  
 
 Post subject: Re: Python
 Post Posted: Mon Jan 07, 2008 1:43 am 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
simply putting balance = 0 infront of the whole thing won't work.

or is that not what you're suggesting?

_________________
Image
Image


Top 
 Profile  
 
 Post subject: Re: Python
 Post Posted: Mon Jan 07, 2008 1:51 am 
Offline
I'm ghey 4 teh Hoff!
User avatar

Joined: Wed Sep 21, 2005 5:18 pm
Posts: 4142
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?)


Top 
 Profile  
 
 Post subject: Re: Python
 Post Posted: Mon Jan 07, 2008 4:16 am 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
sorted - thanks :D

_________________
Image
Image


Top 
 Profile  
 
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 5 posts ] 

Board index » HELP AND ADVICE » GAME/SOFTWARE/HARDWARE PROBLEMS


Who is online

Users browsing this forum: No registered users and 21 guests

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
  • Shoutbox
  • Shout Message


test