Chapter 2 : Truth, Branching, and the Game Loop: Guess My Number

Example 2.1 page no : 41

In [13]:
if (True):
    print  "This is always displayed.\n\n";

if (False):
    print  "This is never displayed.\n\n";
score = 1000;
if (score):
    print  "At least you didn't score zero.\n\n";

if (score >= 250):
    print  "You scored 250 or more. Decent.\n\n";

if (score >= 500):
    print  "You scored 500 or more. Nice.\n\n";
if (score >= 1000):
    print  "You scored 1000 or more. Impressive!\n";
This is always displayed.


At least you didn't score zero.


You scored 250 or more. Decent.


You scored 500 or more. Nice.


You scored 1000 or more. Impressive!

Example 2.2 page no :46

In [2]:
print  "Enter your score: ";
score = 1500 #int(raw_input())
if (score >= 1000):
    print  "You scored 1000 or more. Impressive!\n";
else:
    print  "You scored less than 1000.\n";
Enter your score: 
You scored 1000 or more. Impressive!

Example 2.3 page no : 49

In [3]:
print  "Enter your score: ";
score = 1500 #int(raw_input())

if (score >= 1000):
    print  "You scored 1000 or more. Impressive!\n";
elif (score >= 500):
    print  "You scored 500 or more. Nice.\n";
elif (score >= 250):
    print  "You scored 250 or more. Decent.\n";
else:
    print  "You scored less than 250. Nothing to brag about.\n";
Enter your score: 
You scored 1000 or more. Impressive!

example 2.4 page no : 53

In [4]:
print "Difficulty Levels\n\n";
print "1 - Easy\n";
print "2 - Normal\n";
print "3 - Hard\n\n";

print "Choice: ";
choice = 2 #int(raw_input())
if choice==1:
    print "You picked Easy.\n";
elif choice==2:    
    print "You picked Normal.\n";
elif choice==3:
    "You picked Hard.\n";
else:    
    "You made an illegal choice.\n";
Difficulty Levels


1 - Easy

2 - Normal

3 - Hard


Choice: 
You picked Normal.

example 2.5 page no : 55

In [6]:
choices = ['y','y','n']
again = 'y'
i = 0
while (again == 'y'):
    print  "\n**Played an exciting game**";
    print "\nDo you want to play again? (y/n): ",
    again = choices[i] #raw_input()
    i += 1

print "\nOkay, bye.";
**Played an exciting game**

Do you want to play again? (y/n):  
**Played an exciting game**

Do you want to play again? (y/n):  
**Played an exciting game**

Do you want to play again? (y/n):  
Okay, bye.

example 2.6 page no : 57

In [7]:
choices = ['y','y','n']
i = 0
again = 'y'
while (again == 'y'):
    print  "\n**Played an exciting game**";
    print "\nDo you want to play again? (y/n): ",
    again = choices[i] #raw_input()
    i += 1

print "\nOkay, bye.";
**Played an exciting game**

Do you want to play again? (y/n):  
**Played an exciting game**

Do you want to play again? (y/n):  
**Played an exciting game**

Do you want to play again? (y/n):  
Okay, bye.

example 2.7 page no :59

In [8]:
count = 0;
while (True):
    count += 1;
    #end loop if count is greater than 10
    if (count > 10):
        break;
    #skip the number 5
    if (count == 5):
        continue;
    print count
1
2
3
4
6
7
8
9
10

example 2.8 page no : 63

In [9]:
print "\tGame Designer's Network\n";
success = False
while not success:
    print  "\nUsername: ";
    username = 'guest' #raw_input()
    print  "Password: ";
    password = 'guest' #raw_input()
    if (username == "S.Meier" and password == "civilization"):
        print  "\nHey, Sid.";
        success = True;
    elif (username == "S.Miyamoto" and password == "mariobros"):
        print  "\nWhat's up, Shigeru?";
        success = True;
    elif (username == "W.Wright" and password == "thesims"):
        print  "\nHow goes it, Will?";
        success = True;
    elif (username == "guest" or password == "guest"):
        print  "\nWelcome, guest.";
        success = True;
    else:
        print  "\nYour login failed.";
        success = False;
	Game Designer's Network


Username: 
Password: 

Welcome, guest.

example 2.9 page no : 68

In [10]:
import random

randomNumber = random.random()*100;
#seed random number generator
#generate random number
die = int(randomNumber % 6) + 1; # get a number between 1 and 6
print "You rolled a " , die
You rolled a  4

example 2.10 page no : 74

In [11]:
import random
secretNumber = int(random.random()*100) % 10 + 1;
tries = 0;
#seed random number generator
# random number between 1 and 100
print  "\tWelcome to Guess My Number\n\n";
guesses = [0,1,2,3,4,5,6,7,8,9,10]
i = 0
while True:
    print  "Enter a guess: ";
    guess = guesses[i] #int(raw_input())
    i += 1
    tries += 1
    if (guess > secretNumber):
        print  "Too high!\n\n";
    elif (guess < secretNumber):
        print  "Too low!\n\n";
    else:
        print  "\nThat's it! You got it in " , tries , " guesses!\n";
        break
	Welcome to Guess My Number


Enter a guess: 
Too low!


Enter a guess: 
Too low!


Enter a guess: 
Too low!


Enter a guess: 
Too low!


Enter a guess: 
Too low!


Enter a guess: 
Too low!


Enter a guess: 

That's it! You got it in  7  guesses!