12 Replies - 4303 Views - Last Post: 01 October 2012 - 05:48 PM Rate Topic: -----

#1 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

How to program a coin toss game?

Posted 30 September 2012 - 02:35 PM

need to create a program for class that simulates 100 coin toss flips. for some reason its does not work what is wrong with it?

this is the program i wrote
print ("\n\COIN TOSS SIMULATION MENU")
print ("\t--------------------------------------")
print ("\ta)Simulate 100 coin Tosses")
print ("\tq)Quit the program") 
selection = input("\n\tPlease make a selection: ")
if(selection == 'a'):
import random
count = 0 
heads = 0
tails = 0

while count < 0  

      flip = random.randrange (0,1)
      if flip == 1 
           heads ==1
           print "heads"
      elif flip == 0 
           tails += 1 
           print "tails" 

print ("COIN TOSS STATISTICS")
print ("---------------------")
print("Result")
print("----------")
print("Number of heads:")
print heads
print("Number of tails:") 
print tails 

elif (selection == 'q'); 
print (\n\n "press any keys to exit")



Is This A Good Question/Topic? 0
  • +

Replies To: How to program a coin toss game?

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: How to program a coin toss game?

Posted 30 September 2012 - 02:45 PM

View Postiaremarie, on 30 September 2012 - 02:35 PM, said:

for some reason its does not work what is wrong with it?

What problems are you having with it? We aren't going to run it and find out what's wrong - you have to tell us what problems you're having, specifically.

           heads ==1

Even without being overly familiar with Python, I can tell you that this is going to cause part of whatever problem you're having. Whenever you run into functional issues, you need to go back and carefully re-read everything to look for typos like this.
Was This Post Helpful? 1
  • +
  • -

#3 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How to program a coin toss game?

Posted 30 September 2012 - 02:55 PM

Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate cointoss.py]
Traceback (most recent call last):
  File "c:\Users\asus\cointoss.py", line 8, in <module>
builtins.Indentationerror: expected an indented block (<wingdb_compile>, line 8)


i have a problem with line 8
this is the correct way to put it right?
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to program a coin toss game?

Posted 30 September 2012 - 03:24 PM

Your problem is that lines 7-10 aren't indented after the if statement semicolon which will fix it but, in fact, the entire rest of the program should be indented further than first if statement so there are only coin tosses if the user enters "a". Remember that a Python program relies heavily on indentation.

That's one problem you have few others I can spot, take a look at your while condition, what does it do?, the last elif and what was mentioned in the last post.

This post has been edited by Simown: 30 September 2012 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How to program a coin toss game?

Posted 30 September 2012 - 07:25 PM

i have fixed it but it still does not work
am i using the if/elif/else correctly?

print ("\n\COIN TOSS SIMULATION MENU")
print ("\t--------------------------------------")
print ("\ta)Simulate 100 coin Tosses")
print ("\tq)Quit the program") 
selection = input("\n\tPlease make a selection: ")
if(selection == 'a'):
  else import random
  main def ()
   count = 0 
   heads = 0
   tails = 0

   while (count < 100):   

      flip = random.randrange (0,1)
      if flip == 1:
           heads +=1
           print "heads"
      elif flip == 0 
           tails += 1 
           print "tails" 

print ("COIN TOSS STATISTICS")
print ("---------------------")
print("Result")
print("--------")
print("Number of heads:")
print heads
print("Number of tails:") 
print tails 

if (selection == 'q'):
    elif print (\n\n "press any keys to exit")

Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How to program a coin toss game?

Posted 30 September 2012 - 08:03 PM

You forgot the colon for the elif?
Was This Post Helpful? 0
  • +
  • -

#7 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How to program a coin toss game?

Posted 30 September 2012 - 08:09 PM

View PostfromTheSprawl, on 30 September 2012 - 08:03 PM, said:

You forgot the colon for the elif?


I have a problem with line 8 everytime I debug it, line 8 is the problem i tried to many things to try and fix it
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How to program a coin toss game?

Posted 30 September 2012 - 08:25 PM

I'm not sure if that is possible in Python where in an if statement you create a whole class. Python experts, what's your take on this?

I suggest creating a separate class for your coin toss(or at least a separate method) then call that in your if - else:
def flipCoin():
	//code here
	

def playFlipCoin(playFlip):
		if(playFlip == True):
			flipCoin()
		else:
			pass

Was This Post Helpful? 0
  • +
  • -

#9 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to program a coin toss game?

Posted 30 September 2012 - 08:37 PM

A function definition after an if statement? No, that's not right. It isn't even correct syntax anyway! The def comes before the function name. Don't your errors help you fix it at all?

On line 7 you are missing a colon and and newline after it so import random is in the indented block, line 8 isn't even valid, remove it.

Lines 23-30 need to be at the same indentation level as the while loop.

On line 33 you don't need the elif before the print statement.
Was This Post Helpful? 2
  • +
  • -

#10 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How to program a coin toss game?

Posted 30 September 2012 - 11:29 PM

was able to fix what ever problems need to fix but math portion doesnt work

print ("\n\COIN TOSS SIMULATION MENU")
print ("\t--------------------------------------")
print ("\ta)Simulate 100 coin Tosses")
print ("\tq)Quit the program") 
selection = input("\n\tPlease make a selection: ")
if selection == 'a':
  
    import random
count = 0 
heads = 0
tails = 0

while (count < 100):   

      flip = random.randrange (0,1)
      if flip == 1:
           heads +=1
           print ("heads")
      elif flip == 0:
           tails += 1 
           print ("tails") 

print ("COIN TOSS STATISTICS")
print ("---------------------")
print("Result")
print("--------")
print("Number of heads:")

print("Number of tails:") 


if (selection == 'q'):
    print ("\n\npress any keys to exit")

main()

Was This Post Helpful? 0
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How to program a coin toss game?

Posted 30 September 2012 - 11:43 PM

Try using randint instead of randrange. Also, that import might not work, you have to do it like this:
from random import randint

Was This Post Helpful? 0
  • +
  • -

#12 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to program a coin toss game?

Posted 01 October 2012 - 04:50 AM

Now you've only indented line 8! Indent lines 9-29 to the same level as line 8 now.

And you are calling the main() method on line 35? I don't see a main definition.

Randint and randrange will work equally well in this case. BUT you need to do:
random.randrange(0, 2)

It's not inclusive of the last argument so it stops at 0 and you'll get 0's all the time.

Or change it to:
random.randint(0, 1)


@fromTheSprawl that import syntax is fine, the other option is yours.

This post has been edited by Simown: 01 October 2012 - 04:53 AM

Was This Post Helpful? 1
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How to program a coin toss game?

Posted 01 October 2012 - 05:48 PM

I've got a lot more to learn about Python, thanks. ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1