[Tutor] Basic program question
Timo
timomlists at gmail.com
Mon Jul 25 10:30:04 CEST 2011
On 24-07-11 22:59, Alexander Quest wrote:
> Hello- I am running Python v 3.1.1. As an exercise, I wrote a simple
> coin flipper program, where the computer flips a coin 100 times and
> then prints out the number of heads and tails. My program crashes
> immediately if I run it normally through the command line, but if I go
> to "Run- Run Module," it seems to work just fine. I can't seem to
> figure out why. I've pasted the relevant code below- any help will be
> greatly appreciated. Thanks!
If I read it correctly, you probably *think* it crashes because when you
run this script from the terminal, it will close right after it finishes
so you can't see any output.
See the last line for the solution.
>
> import random
> print("\tWelcome to the 'Coin Flipper' program!")
>
> counter = 0
> heads = 0
> tails = 0
>
> while counter < 100:
> the_number = random.randint(1, 2)
> if the_number == 1:
> heads += 1
> else:
> tails += 1
>
> counter += 1
>
> print("\nI flipped the coint 100 times.")
> print("It came up heads", heads, "times and tails", tails, "times.")
>
> print("\n\nPress the enter key to exit.")
Change this line to:
input("\n\nPress any key to exit.")
Cheers,
Timo
>
> _________________
>
> -Alex
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list