473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
-----------------------
# 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.randrang e(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('pres s enter to exit')
--------------------------------------------

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

peace
Kelvin

Oct 6 '06 #1
8 3244
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.randrang e(1)
list_guess = ['t', 'h']
ini_guess = list_guess[ini_guess]
random.randrang e(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=rando m.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.randrang e(1)
list_guess = ['t', 'h']
ini_guess = list_guess[ini_guess]

random.randrang e(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=rando m.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

Oct 7 '06 #6
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.appe nd(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.
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=rando m.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
Oct 7 '06 #8
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.appe nd(pre_user)
count += 1

correct = 0
import random
ini_guess = random.randrang e(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('pres s enter to exit')

</code>

Thanks for all the people who helped me:)

Peace
Kelvin

Oct 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3037
by: Mel | last post by:
i need to create a unix like "tail" function on my site. the question: the text is displayed in a scrolled area and a new line is added at the end and the entire text is scrolled down so that user can see the last item added. i am trying to avoid displaying an HTML file and have to scroll it down, since it does not seem to work all the time
10
6857
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks I have a function in a <SCRIPT> block that I want in the <head></head> section of the page. Unfortunately, RegisterClientScriptBlock, RegisterStartupScript don't always work, and when they do the script is placed inside the <form> tag (this...
12
4402
by: s99999999s2003 | last post by:
hi I have a file which is very large eg over 200Mb , and i am going to use python to code a "tail" command to get the last few lines of the file. What is a good algorithm for this type of task in python for very big files? Initially, i thought of reading everything into an array from the file and just get the last few elements (lines) but since it's a very big file, don't think is efficient. thanks
12
2849
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
52
5395
by: celerysoup16 | last post by:
I've written this coin toss program, and can't figure out why it isn't giving accurate results... cheers, Ben #include <stdlib.h> #include <stdio.h> #define H 1 #define T 0 #define SENTINEL -1
31
4977
by: Manfred Kooistra | last post by:
If I have a document like this: <html> <head> <script language=javascript> window.location.href='file.php'; </script> </head> <body> body content
6
6075
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main () { int coin, counter, tails = 0, heads = 0; for (counter = 1; counter <= 100; counter++)
3
4540
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well for the most part, but the problem is when attempting to continuously pipe information into the application via the tail -f command. The command line looks something like this: tail -f <logfile| grep <search string| python parse.py
18
3309
by: Chinde | last post by:
Hi I'm using AS2 to produce a simple platform game. So far so good I would like the character to collect coins or whatever. so what I have done is create a coin movie clip, I've run a hittest on the coin and my character and when this = true set the coin to _visible = false, move it way off screen and add 10 point to the _global.score. all is working fine but I haven't a clue about having more than one instance of the coin, or more precisely I...
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.