[Tutor] Loop

Alexandre Conrad alexandre.conrad at gmail.com
Sun Jun 5 00:48:52 CEST 2011


Vincent,

You will need to move the line that flips the coin:

coin = random.randint(1,2)

and the if/else logic *inside* the loop so it will be repeated as many
times as needed.

I'm not sure what you want to do with the input() inside the loop. Remove this.

Alex



2011/6/4 Vincent Balmori <vincentbalmori at yahoo.com>:
> Hello. Right now I am learning the python language through Python
> Programming for the Absolute Beginner 3rd Edition. I am having trouble with
> one question in Ch. 3 #2, which says "Write a program that flips a coin 100
> times and then tells you the number of heads and tails." Right now I am
> having trouble connecting the random.randint function into the loop.
>
> # Coin
> import random
> # Set up Values
> coin = random.randint(1,2)
> tails = 0
> heads = 0
> if (coin == 1):
>     tails += 1
> else:
>     heads += 1
> toss = 0
> while toss < 100:
>     toss += 1
>     coin = input(random.randint(1,2)
>
> print("Toss Count:", count)
> print("Tails:", tails)
> print("Heads:", heads)
> input("\n\nPress the enter key to exit.")
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Alex | twitter.com/alexconrad


More information about the Tutor mailing list