CSC111 Homework 4 Solutions 2014
--D. Thiebaut (talk) 12:33, 6 March 2014 (EST)
Problem 1
Version 1
#hw4a
#Nicole Wong (bw)
#Anna Render (bp)
#(slightly edited by D. Thiebaut)
#
# Rock, Paper, Scissors game
# The program will stop only once the difference in score between computer and human players is 3
# The program prompts the user if the input is not one of 'P', 'R', or 'S'.
# The program will accept valid letters in lower or upper case, and with extra spaces before or after the letter.
# The program will indicate the winner of each round.
# The program will stop and output a final message at the end, when the difference in score is 3,
# pointing out who the winner is.
#define variables
from random import choice
from random import seed
seed()
# constant values used in game
OPTIONS = ['R', 'P', 'S']
# counters for score
computerScore = 0
humanScore = 0
difference = 0
#--- while the difference between the human score and computer score is less than 3---
while difference < 3:
computer = choice( OPTIONS )
human = input( "What is your play? (R, P, S) " ).upper().strip()
#--- keep prompting as long as invalid input ---
while human != 'P' and human != 'S' and human != 'R':
human = input( "I didn't get that. Please reenter: " ).upper().strip()
#--- feedback to user ---
print( "You have played: ", human.upper() )
print( "The computer has chosen: ", computer )
if ( computer == 'R' and human == 'P' ) \
or ( computer == 'P' and human == 'S' ) \
or ( computer == 'S' and human == 'R' ) :
print( "Whatever, you win this round." )
print()
humanScore = humanScore + 1
difference = abs(humanScore - computerScore)
elif ( computer == 'R' and human == 'S' ) \
or ( computer == 'P' and human == 'R' ) \
or ( computer == 'S' and human == 'P' ) :
print( "Haha, I win this round!" )
print()
computerScore = computerScore + 1
difference = abs(humanScore - computerScore)
else:
print( "It's a tie!" )
print()
difference = abs(humanScore - computerScore)
#--- once the difference between the human and computer score is 3 ---
#--- print winner ----
if computerScore > humanScore:
print( "You lose the game! I am sorry, but you are no match for me!" )
else:
print( "Congrats, you win the game!" )
# (Note: there can't be a tie because the main loop repeats until difference of 3)