Table of Contents

Basics II: Random Numbers

The second thing I taught the class was random numbers.

import random

x = random.randint(1,10)

print(x)

What is the answer? The students were amazed when the number was different each time!

Then I said, let's combine the two ideas, and we did this:

import random

x = random.randint(1,31)

if x == 31:
    print("Appledog")
elif x == 13:
    print("Tim")
elif x == 17:
    print("Ray")
else:
    print(x)

This program printed a name, of their friend, or, the student number. It was the focus of the entire class, to let them play with this idea.

Their homework was to write a “scissor, paper, stone” game. Many of them finished it within 1 class.

Homework

Write a scissor paper stone game.

Answer

import random

while True:
    print ("a. Scissor")
    print ("b. Paper")
    print ("c. Stone")
    print ("q. quit")
    
    guess = input("Your guess (abcq)? ")
    answer = random.randint(1,3)
    
    if guess == 'q':
        quit()
        
    if answer == 1:
        print("The computer chose scissor!")
    
    if answer == 2:
        print("The computer chose paper!")
   
    if answer == 3:
        print("The computer chose stone!")
        

Answer part II

    if answer == 1:
        print("The computer chose scissor!")
        if guess == 'a':
            print("It's a tie!")
        elif guess == 'b':
            print("You win!")
        else:
            print("You lose!")

    # and so forth

The answers may be similar but point out that the logic can be condensed.