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 8 3151
Um... It looks to me like it just counts the number of times you
entered 't'...
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'...
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.
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.
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
# 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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
...
|
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...
|
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
|
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...
|
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
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
| |