469,610 Members | 1,710 Online

# humble coin head or tail game script I wrote

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:
-----------------------
#
# 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\'

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

Oct 6 '06 #1
8 3038
Um... It looks to me like it just counts the number of times you
entered 't'...

Oct 6 '06 #2
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'...
Oct 6 '06 #3
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.

Oct 6 '06 #4
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':
...

sj*******@yahoo.com 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.
Oct 7 '06 #5
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!

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

Oct 7 '06 #6
# 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\'

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
You can simplify this considerably:

user_input = [] # list is a keyword so makes a bad variable name.

while len(user_input) < 10:
print '\nNo.', len(user_input) + 1, ', h or t? '
pre_user = raw_input()
if pre_user not in ["h","t"]: # Note you can also use <code>not
in "ht"</code>
print '\njust enter \'h\' or \'t\' please'
continue
user_input.append(pre_user)

print user_input

HTH.

Oct 7 '06 #7
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.

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!

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
Oct 7 '06 #8
OK so this is the result after I taking everything I'm teached in this

<code>
print 'Predict Head or Tail in ten times coin thrown\nJust input \'h\'

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')

</code>

Thanks for all the people who helped me:)

Peace
Kelvin

Oct 7 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.