CSC111 Week 8 Programs 2015
--D. Thiebaut (talk) 13:19, 25 March 2015 (EDT)
# week8_2.py
# D. Thiebaut
# program written in class on 3/25/15
# First function is a guessing game
# Second function is a game of rock-paper-scissors
#
import random
# plays a game. Picks a random number (1-100). Asks the
# user to guess what it is, and reports if too low, to high,
# or match.
def guessingGame():
computer = 50 # random.randrange( 1, 101)
# friendly explanations
print( "Let's play... guess the number I picked..." )
while True==True:
# get user's guess
# report on too low or too high
user = int( input( "Enter a number between 1 and 100> " ) )
# compare user's guess to computer pick
if user == computer:
print( "You guessed it!\nI had picked", computer )
break
else:
if user < computer:
print( "Too low!" )
else:
print( "Too high!" )
# winner: given two letters that are "R", "P" or "S",
# apply the rules of Rock, Paper, Scissors to indicate
# which one wins. Returns COMPUTERWINS if first letter
# wins, USERWINS if second letter, and TIE if neither.
def winner( computer, user ):
TIE = "TIE"
COMPUTERWINS = "COMPUTERWINS"
USERWINS = "USERWINS"
# do we have a tie?
if computer==user:
return TIE
# if not, figure out who wins and return the appropriate
# constant
if ( computer=="R" and user=="S"
or computer=="S" and user=="P"
or computer=="P" and user=="R" ):
return COMPUTERWINS
else:
return USERWINS
# playRockPaperScissors: plays a whole game
def playRockPaperScissors():
# define constants
TIE = "TIE"
COMPUTERWINS = "COMPUTERWINS"
USERWINS = "USERWINS"
# initialize variables
computCount = 0
userCount = 0
# play the game
while True==True:
# pick a random letter for the computer
computer = random.choice( [ "R", "S", "P" ] )
# get the user input (careful, this is totally not robust!)
# the user can enter anything she wants...
user = input( "\n\nYour play? " ).upper()
# Report outcome to the user
if winner( computer, user )==TIE:
print( "It's a tie!" )
else:
if winner( computer, user )==COMPUTERWINS:
print( "I win! I had picked", computer )
computCount += 1
else:
print( "You win! I had picked", computer )
userCount += 1
# print scores:
print( "Scores: user:", userCount, " computer:", computCount )
def main():
#while True:
# guessingGame()
# ans = input( "Play again (Y/N)? " )
# if not ans in [ "y", "Y", "yes", "YES" ]:
# break
playRockPaperScissors()
main()