Example solutions for script 04_looping exercise block 8

Exercise 8.1

import random

user = input("your move (R, S or P): ")

computer_move_as_int = random.randint(0, 2)
if computer_move_as_int == 0:
    computer = "R"
elif computer_move_as_int == 1:
    computer = "S"
else:
    computer = "P"
    
print("I play", computer)

if user == computer:
    print("it's a tie")
elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
     print("you win")
else:
    print("I win")
your move (R, S or P): R
I play R
it's a tie

Exercise 8.2

import random

while True:
    user = input("your move (R, S or P): ")
    if user == "R" or user == "S" or user == "P":
        break
    print("your input is not valid, try again !")
    print()

computer_move_as_int = random.randint(0, 2)
if computer_move_as_int == 0:
    computer = "R"
elif computer_move_as_int == 1:
    computer = "S"
else:
    computer = "P"
    
print("I play", computer)

if user == computer:
    print("it's a tie")
elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
    print("you win")
else:
    print("I win")
your move (R, S or P): a
your input is not valid, try again !

your move (R, S or P): b
your input is not valid, try again !

your move (R, S or P): R
I play R
it's a tie

Comment: The paraenthesis after elif are not needed, as and is evaluated before or. But as this can be forgotten easily or others who read the code are not aware of this I recommend to use them.

Exercise 8.3

import random

while True:
    while True:
        user = input("your move (R, S or P, X to stop playing): ")
        if user == "R" or user == "S" or user == "P" or user == "X":
            break
        print("your input is not valid, try again !")
        print()
        
    if user == "X":
        break

    computer_move_as_int = random.randint(0, 2)
    if computer_move_as_int == 0:
        computer = "R"
    elif computer_move_as_int == 1:
        computer = "S"
    else:
        computer = "P"

    print("I play", computer)

    if user == computer:
        print("it's a tie")
    elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
        print("you win")
    else:
        print("I win")
    print()
your move (R, S or P, X to stop playing): R
I play S
you win

your move (R, S or P, X to stop playing): R
I play S
you win

your move (R, S or P, X to stop playing): x
your input is not valid, try again !

your move (R, S or P, X to stop playing): X

Exercise 8.4

import random

user_wins = 0
computer_wins = 0 
ties = 0

while True:
    while True:
        user = input("your move (R, S or P, X to stop playing): ")
        if user == "R" or user == "S" or user == "P" or user == "X":
            break
        print("your input is not valid, try again !")
        print()
        
    if user == "X":
        break

    computer_move_as_int = random.randint(0, 2)
    if computer_move_as_int == 0:
        computer = "R"
    elif computer_move_as_int == 1:
        computer = "S"
    else:
        computer = "P"

    print("I play", computer)

    if user == computer:
        print("it's a tie")
        ties = ties + 1
    elif (user == "R" and computer == "S") or (user == "S" and computer == "P") or (user == "P" and computer =="R"):
        print("you win")
        user_wins = user_wins + 1        
    else:
        print("I win")
        computer_wins = computer_wins + 1
    print()
    
print()
print("the final result is:")
print("   you won", user_wins, "games")
print("   I won", computer_wins, "games")
print("   we had", ties, "ties")
print()
print("so the overall winner is: ", end="")
if user_wins > computer_wins:
    print("you !")
elif user_wins < computer_wins:
    print("me !")
else:
    print("the two of us !")
your move (R, S or P, X to stop playing): R
I play S
you win

your move (R, S or P, X to stop playing): R
I play R
it's a tie

your move (R, S or P, X to stop playing): R
I play P
I win

your move (R, S or P, X to stop playing): X

the final result is:
   you won 1 games
   I won 1 games
   we had 1 ties

so the overall winner is: the two of us !