[Tutor] Creating Adventure Games Python Port - Chapter 2

Dick Moores rdm at rcblue.com
Mon Oct 2 02:02:11 CEST 2006


At 04:38 PM 10/1/2006, Will Shattuck wrote:
>Hi all,
>
>I thought that it would be a great idea to learn Python while porting
>the BASIC code from this book.  So I printed all the pages, have them
>"bound" by a big binder clip, and read through the first bit.
>
>In Chapter 2 there is a type of "Choose Your Own Adventure" type
>section.  Since I didn't have two quarters, but I did have my laptop,
>I put together a quick "pycoin.py" program to "flip" the coins.  Since
>I am an extreme beginner I thought I would submit it for review.  Here
>you go:
>
>=============================
>import random
>
>coin1 = 0
>coin2 = 0
>
>coin1 = random.randint(1, 10)
>coin2 = random.randint(1, 10)
>
>if coin1 <= 5:
>     print "Coin1 is Heads"
>else:
>     print "Coin1 is Tails"
>
>if coin2 <= 5:
>     print "Coin2 is Heads"
>else:
>     print "Coin2 is Tails"
>=============================

Works fine, but instead of
coin1 = random.randint(1, 10)
coin2 = random.randint(1, 10)

you could simplify by using
coin1 = random.randrange(2)
coin2 = random.randrange(2)

and adjusting the rest of your code accordingly.

Dick Moores





More information about the Tutor mailing list