humble coin head or tail game script I wrote

2006-10-06 Thread Camellia
Hi there this is an easy game which was inspired from my psychology
class.

I'll get 5/10 right prediction of your guess of head and tail at most
time.
If you could copy the code and run it that would be great:)

code:
---
# Head or Tail
#
# Get a list which contains 10 values from the user
# let them predict Head Or Tail in ten times coin thrown
# and then prdict the list by a fixed rule


list = []

print 'Predict Head or Tail in ten times coin thrown\nJust input \'h\'
or \'t\' please\n'

count = 0
while True:
count += 1
print '\nNo.', count, ', h or t? '
pre_user = raw_input()
while pre_user != 'h' and pre_user != 't':
print '\njust enter \'h\' or \'t\' please'
print '\nNo.', count, ', h or t? '
pre_user = raw_input()
list.append(pre_user)
if count == 10:
break

correct = 0
import random
ini_guess = random.randrange(1)
list_guess = ['t', 'h']
ini_guess = list_guess[ini_guess]
# generate random initial guess
for item in list:
if item == ini_guess:
correct += 1
elif item == 'h':
ini_guess = 't'
elif item == 't':
ini_guess == 'h'

print '\n\nI got', correct, 'out of 10 correct.'

raw_input('press enter to exit')


I know it looks stupid, but it's fun:)

peace
Kelvin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: humble coin head or tail game script I wrote

2006-10-06 Thread Camellia
Well...It' doesn't, have you run it yet?

its hypothesis is people don't predict a set of things randomly.


Oxyd wrote:
> Um...  It looks to me like it just counts the number of times you
> entered 't'...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: humble coin head or tail game script I wrote

2006-10-06 Thread Camellia
Oh I get it and ashamed, thank you for explaining it to me:)

so I sould:
ini_guess=random.randrange(2)
...
for item in  list:
if item=='h':
...
if item ==t':
...

[EMAIL PROTECTED] wrote:
> Camellia wrote:
> > Well...It' doesn't, have you run it yet?
>
> Yes it does, and running it reflects that behavior.
>
> > ini_guess = random.randrange(1)
> > list_guess = ['t', 'h']
> > ini_guess = list_guess[ini_guess]
>
> random.randrange(1) will always return 0, so this will always
> initialize ini_guess to 't'
>
> > for item in list:
> > if item == ini_guess:
> > correct += 1
> > elif item == 'h':
> > ini_guess = 't'
> > elif item == 't':
> > ini_guess == 'h'
>
> If item was 't', then correct is incremented and nothing else happens.
> ini_guess remains 't'.
> If item was 'h', ini_guess will be set to 't'.
> 
> The final "elif item=='t':" branch will never be executed.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: humble coin head or tail game script I wrote

2006-10-07 Thread Camellia
Steve Holden thank you for your kind words, they pumped me up:)
I don't really know much about TDD however I googled it and found this:
http://www.agiledata.org/essays/tdd.html
Which is obvious too complicated. However I'll read through it anyway.
Thank you for your advice:)

Ant thank you for pointing that out, I made the little code too
complicated.
Well, actually this is the simplified version, the first one I did was
like:
list_1 = raw_input()
list_2 = raw_input()
...
list_10 = raw_input()
and then I found I'm doing the computer's work...

Thanks for all the kind people here
Peace
Kelvin


Steve Holden wrote:
> Camellia wrote:
> > Oh I get it and ashamed, thank you for explaining it to me:)
> >
> > so I sould:
> > ini_guess=random.randrange(2)
> > 
> > for item in  list:
> > if item=='h':
> > ...
> > if item ==t':
> > ...
> >
> Welcome to programming. You have learned, as many thousands have learned
> before you, how easy it is to assume correct behaviour in something that
> is, in fact, wrong. Those with an aptitude for the task accept with
> humility (no need for shame, though, as inexperience is a valid excuse)
> that they will continue to make errors they do not see.
>
> Your response to the corrections you received implies you have a future
> as a programmer!
>
> You might also want to do some reading about test-driven development,
> which can save some time hunting obscure bugs.
>
> regards
>   Steve
> --
> Steve Holden   +44 150 684 7255  +1 800 494 3119
> Holden Web LLC/Ltd  http://www.holdenweb.com
> Skype: holdenweb   http://holdenweb.blogspot.com
> Recent Ramblings http://del.icio.us/steve.holden

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: humble coin head or tail game script I wrote

2006-10-07 Thread Camellia
OK so this is the result after I taking everything I'm teached in this
thread:


print 'Predict Head or Tail in ten times coin thrown\nJust input \'h\'
or \'t\' please\n'

count = 0
user_input = []

while len(user_input) < 10:
print '\nNo.', len(user_input)+1, ', h or t?'
pre_user = raw_input()
if pre_user not in ['t', 'h']:
print '\njust enter \'h\' or \'t\' please'
continue
user_input.append(pre_user)
count += 1

correct = 0
import random
ini_guess = random.randrange(2)
list_guess = ['t', 'h']
ini_guess = list_guess[ini_guess]
# generate random initial guess
for item in user_input:
if item == 'h':
if ini_guess == item:
correct += 1
else:
ini_guess = 'h'
if item == 't':
if ini_guess == item:
correct += 1
else:
ini_guess = 't'

print '\n\nI got', correct, 'out of 10 correct.'

raw_input('press enter to exit')



Thanks for all the people who helped me:)

Peace
Kelvin

-- 
http://mail.python.org/mailman/listinfo/python-list


UnboundLocalError

2006-11-09 Thread Camellia
hi all

why it generates an "UnboundLocalError" when I do the following:


...
def main():
number = number()
number_user = user_guess()
while number_user != number:
check_number(number = number, number_user = number_user)
number_user = user_guess()

UnboundLocalError: local variable 'number' referenced before assignment


I found when I changed the number() to num() or whatever the issue
solved
but doesn't every function has its own namespace?
Can anyone please explain it to me?

Peace

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError

2006-11-10 Thread Camellia
Thank you all so much for all the replies:)

But sorry I'm so dumb I can't say I really understand,
what do I actually do when I define a function with its name "number"?
why does a name of a function has something to do with a variable?

Oh wait can I do this in Python?:

def a():
def b()


so the b() will appear to be a local function which is the possible
cause of my little own error because the compiler will interpret the
number() as a local function but a global one?



On Nov 10, 7:32 am, Rob Williscroft <[EMAIL PROTECTED]> wrote:
> Terry Reedy wrote innews:[EMAIL PROTECTED]
> comp.lang.python:
>
> >> def main():
> >>number = number()
>
> > Within a function, a given name can be either global or local, but not
> > both.
> > Here you are expecting the compiler to interpret the first occurance
> > of 'number' as local and the second as global.  Humans can often
> > resolve such ambiguities correctly, but not necessarily always.  So
> > this is too much to ask of a program and hence it is not allowed.".. asked 
> > too much of the programme", sounds like a BOFH excuse to
> me ;-).
>
> Seriously I'd bet (if I were a gambling man) that this is by design,
> not either of "too much work for the interpreter" or "nobody's
> submitted a patch".
>
> IOW: Why should the intepreter do more work just so the user
> can find new and interesting ways to shoot them selves in the foot.
> 
> Rob.
> --http://www.victim-prime.dsl.pipex.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError

2006-11-11 Thread Camellia
Oh how can I thank you enough, you make my day:)
According to what you said I finally figure it out, it is the same as:


b = 1
def a():
b = b #no good:)


So in every day programming I should avoid using the same name for
different objects because they will step on each other, right?

On Nov 11, 6:18 pm, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> At Saturday 11/11/2006 02:35, Camellia wrote:
>
> >But sorry I'm so dumb I can't say I really understand,
> >what do I actually do when I define a function with its name "number"?Don't 
> >apologize, Python is a lot more dumb than you. It obeys very
> simple rules (a good thing, so we can clearly understand them, once
> we know them).
> Recalling your previous example:
>
> > > >> def main():
> > > >>number = number()Python first scans the source code looking for 
> > > >> assigned-to names.
> That is, names to the left of equal signs. Those names, plus the
> function formal parameters, make the list of "local names". Any other
> names referenced are assumed to be globals, that is, living outside
> your function. (That's not entirely true but enough for now).
> Notice that "number" is a local name because it's assigned to; it
> doesn't matter whether a global name "number" exists or not.
> When the code is executed, the "number" to the right references the
> local name, which has not been assigned to yet - that's why you get
> an UnboundLocalError. "number" can't be a global name when used on
> the right hand side, and a local name when used on the left hand
> side. It's simple: it is local, or not, but not both.
>
> >why does a name of a function has something to do with a variable?Notice 
> >also that it does not matter *what* kind of object a name
> refers to: it may be a function, a class, an object instance,
> whatever. Inside a function, by example, a local name "a" may be
> bound at most to a single object at a time, it doesn't matter its
> type. A local name "a" can't refer to an integer and a class at the same time.
> The left hand side of an assign statement *always* uses local names,
> except when you declare a name to be global by using the "global" keyword.
> And notice also that I've never used the word variable.
>
> >Oh wait can I do this in Python?:
> >
> >def a():
> > def b()
> >
>
> >so the b() will appear to be a local function which is the possible
> >cause of my little own error because the compiler will interpret the
> >number() as a local function but a global one?Yes, you can. Python has 
> >"lexically nested scopes". The simple
> local/global rule of above is a bit more complicated: when a name is
> not local, it is searched inside the enclosing functions, then in the
> containing module's global namespace, and last in the builtin names.
> And yes, b is local to a, so it effectively hides any external b that
> could be in outer scopes.
>
> --
> Gabriel Genellina
> Softlab SRL
>
> __
> Correo Yahoo!
> Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
> ¡Abrí tu cuenta ya! -http://correo.yahoo.com.ar

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError

2006-11-11 Thread Camellia
Oh how can I thank you enough, you make my day:)
According to what you said I finally figure it out, it is the same as:


b = 1
def a():
b = b #no good:)


So in every day programming I should avoid using the same name for
different types of objects because they will step on each other, right?

On Nov 11, 6:18 pm, Gabriel Genellina <[EMAIL PROTECTED]> wrote:

On Nov 11, 6:18 pm, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> At Saturday 11/11/2006 02:35, Camellia wrote:
>
> >But sorry I'm so dumb I can't say I really understand,
> >what do I actually do when I define a function with its name "number"?Don't 
> >apologize, Python is a lot more dumb than you. It obeys very
> simple rules (a good thing, so we can clearly understand them, once
> we know them).
> Recalling your previous example:
>
> > > >> def main():
> > > >>number = number()Python first scans the source code looking for 
> > > >> assigned-to names.
> That is, names to the left of equal signs. Those names, plus the
> function formal parameters, make the list of "local names". Any other
> names referenced are assumed to be globals, that is, living outside
> your function. (That's not entirely true but enough for now).
> Notice that "number" is a local name because it's assigned to; it
> doesn't matter whether a global name "number" exists or not.
> When the code is executed, the "number" to the right references the
> local name, which has not been assigned to yet - that's why you get
> an UnboundLocalError. "number" can't be a global name when used on
> the right hand side, and a local name when used on the left hand
> side. It's simple: it is local, or not, but not both.
>
> >why does a name of a function has something to do with a variable?Notice 
> >also that it does not matter *what* kind of object a name
> refers to: it may be a function, a class, an object instance,
> whatever. Inside a function, by example, a local name "a" may be
> bound at most to a single object at a time, it doesn't matter its
> type. A local name "a" can't refer to an integer and a class at the same time.
> The left hand side of an assign statement *always* uses local names,
> except when you declare a name to be global by using the "global" keyword.
> And notice also that I've never used the word variable.
>
> >Oh wait can I do this in Python?:
> >
> >def a():
> > def b()
> >
>
> >so the b() will appear to be a local function which is the possible
> >cause of my little own error because the compiler will interpret the
> >number() as a local function but a global one?Yes, you can. Python has 
> >"lexically nested scopes". The simple
> local/global rule of above is a bit more complicated: when a name is
> not local, it is searched inside the enclosing functions, then in the
> containing module's global namespace, and last in the builtin names.
> And yes, b is local to a, so it effectively hides any external b that
> could be in outer scopes.
>
> --
> Gabriel Genellina
> Softlab SRL
>
> __
> Correo Yahoo!
> Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
> ¡Abrí tu cuenta ya! -http://correo.yahoo.com.ar

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError

2006-11-11 Thread Camellia
Oh thank you for pointing that out Fredrik, you made the case more
clear:)

On Nov 11, 7:19 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Camellia wrote:
> > Oh how can I thank you enough, you make my day:)
> > According to what you said I finally figure it out, it is the same as:
>
> > 
> > b = 1
> > def a():
> > b = b #no good:)
> > if you really want to modify a variable that lives outside the 
> > function,
> you can use the "global" directive:
>
> http://effbot.org/pyfaq/how-do-you-set-a-global-variable-in-a-functio...
>
>  > So in every day programming I should avoid using the same name for
>  > different types of objects because they will step on each other,
>  > right?
>
> if you want to distinguish between things, giving them distinct names is
> always a good idea.
> 
> 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to return an "not string' error in function?

2006-09-21 Thread Camellia
I've learned a lot from you two, thank you:)

Peace

-- 
http://mail.python.org/mailman/listinfo/python-list