humble coin head or tail game script I wrote

C

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
 
C

Camellia

Well...It' doesn't, have you run it yet?

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

sjdevnull

Camellia said:
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.
 
C

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':
...

Camellia said:
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.
 
S

Steve Holden

Camellia said:
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
 
A

Ant

# 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

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.
 
C

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
 
C

Camellia

OK so this is the result after I taking everything I'm teached in this
thread:

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

</code>

Thanks for all the people who helped me:)

Peace
Kelvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top