[Tutor] blackJack problem

Alan Gauld alan.gauld at btinternet.com
Wed Oct 24 02:23:30 CEST 2012


On 24/10/12 01:00, Matthew D wrote:

>  > > I thought by having a value outside
>  > > of the functions made the value global.
>  >
> ok...is there an easier way to write this? the reason i was calling that
> global value the same as the currentBet is because i need to add that
> value to 5...

You need to read about global variables and namespaces in Python.

But using a global name as a parameter name will not make the function 
use the global name as the first parameter, in fact the opposite will 
happen in that the parameter will hide the global value.

If you want to pass the global value into your function then call it 
with the global name as the first argument. If you want to change the 
global value from your function then you need to declare the name global 
inside your function:

##########
readMe = 42   # global var we will read
changeMe=666  # global valuer we will change

def foo(param1):
     print param1
     print readMe    # accesses the global value

def bar():
    global changeMe   # use global value rather than local
    print 'old: ', changeMe
    changeMe = 99
    print 'new: ', changeMe

print readMe, changeMe   # print original values

foo(readMe)  #prints 42 twice, once as param1, once as global value

bar()   # prints old and new values

print readMe, changeMe    # print after values

##################

> l...still cant get even a slight idea as of what should work

Try the above and variations. If you are still stuck ask here for more 
detail.

You can also try reading the "Whats in a Name?" topic of my tutorial for 
a different explanation.

>  > > peoples "player1.py" files on my game.
>  >
>  > Interesting concept. And if the other peoples files are calling your
>  > functions with values in those parameters you will just ignore them I
>  > presume? Hmmm, that should produce some interesting results...
> it should work in theory correct?

No, in theory users will get unpredictable results since other people 
will assume that the values they pass into your functions will influence 
the result. They will not expect you to ignore them and apply some 
totally arbitrary values of your own. The theory is that the parameters 
in a function control the behaviour, not act as simply placeholders for 
random data. If your teacher wants the function to have certain 
parameters I suspect he wants you to use them in your function!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list