473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

another beginner game question

15
Hi all

Its me again. This time I want to create a program that flips a coin 100 times
and tells you the number of heads and tails

I tried using an intentional infinite loop but it didn't turn out right
please give some advice

thanks
Mar 11 '08 #1
7 1000
elcron
43
Hi all

Its me again. This time I want to create a program that flips a coin 100 times
and tells you the number of heads and tails

I tried using an intentional infinite loop but it didn't turn out right
please give some advice

thanks
Can you post some code. Use a for loop for the loop.
Expand|Select|Wrap|Line Numbers
  1. def flipCoin():
  2.     #...
  3.  
  4. for i in xrange(100):
  5.     print flipCoin()
  6.  
Mar 12 '08 #2
Hamy Li
15
Can you post some code. Use a for loop for the loop.
Expand|Select|Wrap|Line Numbers
  1. def flipCoin():
  2.     #...
  3.  
  4. for i in xrange(100):
  5.     print flipCoin()
  6.  


Well I first tried to make it print out the 100 times of coin flip

Expand|Select|Wrap|Line Numbers
  1. import random
  2. tries = 1
  3. while True:
  4.     side = random.randrange(2)
  5.     tries += 1
  6.     if tries > 100:
  7.         break
  8.  
  9. if side == 0:
  10.     print "Heads"
  11. elif side == 1:
  12.     print "tails"
  13. else:
  14.     print "None"
  15. print side
  16.  
  17.  
  18. raw_input("\n\nPress the Enter key to exit")

but it didn't turn out so well
Mar 12 '08 #3
bgeddy
16
Well going through your code - here are some observations.

Firstly you do not accumulate the numer of heads and tails occurring so you have no count to show. Secondly, as stated, if you are simply doing something a set number of times in a loop a 'for num in xrange(loops)'
is the simplest construct.

Even if using a while loop as you have, you don't need an infinite loop and a break, in your case "while tries<=100" would be better. Then you drop the indentation so only the last side tossed will be printed and as side can only be 0 or 1 you don't need the 'else : print 'None'.

The code here may give you some ideas - I've included a dictionary, a tuple and a subscript and an augmented assignment when they're not strictly necessary to give you some things to check up on !

Expand|Select|Wrap|Line Numbers
  1. import random
  2. tosses={"Heads":0,"Tails":0}
  3. for i in xrange(100):
  4.     tosses[("Heads","Tails")[random.randrange(2)]]+=1
  5. print tosses
Hope this helps..
Mar 12 '08 #4
Hamy Li
15
thanks alot for you time.

and what does xrange do?
Mar 12 '08 #5
jlm699
314 100+
and what does xrange do?
Same thing as range() only instead of returning a list it's more of an as-needed int ... it's better for memory efficiency and faster than range in loops..
Mar 12 '08 #6
bgeddy
16
I'm not sure what is this site's policy on giving URL's so I'll just say if you were to google "python411" you will find a site with loads of tutorials/books/links etc for python.

Read up as much as you can but always have a python interpreter fired up to play with ! You will learn much more by experimentation of concepts.

At the python prompt you can type :

Expand|Select|Wrap|Line Numbers
  1. help(xrange) 
to get an idea what this does (althought you may find the information cryptic at first).

Good Luck !!
Mar 13 '08 #7
Hamy Li
15
thanks alot. The website sure helped me
Mar 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lee Garrington | last post by:
Hey, Recently I decided to learn C++ so that I could port over one of my Java programs to make it faster. Basically everything has ported over fine so far until I came up against the following...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
17
by: Barret Bonden | last post by:
As an old programmer just now looking at VB.net I have a question: How does one simply open one form from another ? I don't mean how does one create a new instance of that form , but rather how...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
11
by: Simon | last post by:
I have a quick question on the Mersenne Twister (hereinafter MT) I'm using the standard C code downloaded from the MT website (http://tinyurl.com/6d8t3). It's being used for a game to generate...
2
by: theronnightstar | last post by:
I am writing an anagram program for my fiance. Figured it would be an excellent task to learn from. The way it is supposed to work is it reads in a word list from a file into a temporary...
0
by: Shawn Minisall | last post by:
For my final project, I'm trying to do a GUI based game similar to are you smarter then a 5th grader. I've been working on it and am stuck with some code someone helped me with to randomize the...
13
by: Hamy Li | last post by:
In a book that I'm learning, there is a project which I have to create a game called "Guess my number". In this game, I pick a number from 1 to 100 and let the computer guess my number. I don't know...
5
by: alesitaam | last post by:
Help!!!! Im new using python, currently writing a program which tests one game, IQ test. When the module is run, the program should ask user to choose the game to start. Also, I'm using Try...Except...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.