CSC111 Homework 3 Solution Programs 2014

From dftwiki3
Revision as of 10:37, 27 February 2014 by Thiebaut (talk | contribs) (Program 1)
Jump to: navigation, search

--D. Thiebaut (talk) 10:36, 27 February 2014 (EST)


Program 1


An elegant solution that simply replicates the code for each round. We hadn't seen the while loop yet, so this was perfectly acceptable. The code is well organized and easy to scan/read.

# hw3a.py
# by Zoey Jingyi Sun(ch) and Phyllis Chen(cv)
# (slightly edited by D. Thiebaut)
#
# This program allow users to play a more advanced version of
# "ROCK/PAPER/SCISSORS/MUG" by adding a MUG option. The rules are "Scissors fall in mug, and mug wins
#  Paper blocks mug, and paper wins,Rock breaks mug, and rock wins". This program
#  will let the user play three rounds and keep track of the score. In the end, it
#  will declare the winner.
# 

#Initialized the computerscore and humanscore
computerScore = 0
humanScore = 0

# ========================================================
# Round1

from random import choice

OPTIONS = ['R', 'P', 'S' ,'M']

computer = choice( OPTIONS )
human = input( "What is your play? " ).replace(" ","").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')\
   or ( computer=='M' and human=='P')\
   or ( computer=='M' and human=='R')\
   or ( computer=='S' and human=='M'):
    
    print( "Congrats, you win!" )
    humanScore = humanScore + 1
       
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P' )\
   or ( computer=='M' and human=='S')\
   or ( computer=='R' and human=='M')\
   or ( computer=='P' and human=='M'):
    print( "Sorry, you lose this time!" )
    computerScore = computerScore + 1
else:
    print( "It's a tie!" )

# ========================================================
#Round2


computer = choice( OPTIONS )
human = input( "What is your play? " ).replace(" ","").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')\
   or ( computer=='M' and human=='P')\
   or ( computer=='M' and human=='R')\
   or ( computer=='S' and human=='M'):
    
    print( "Congrats, you win!" )
    humanScore = humanScore + 1
       
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P' )\
   or ( computer=='M' and human=='S')\
   or ( computer=='R' and human=='M')\
   or ( computer=='P' and human=='M'):
    print( "Sorry, you lose this time!" )
    computerScore = computerScore + 1
else:
    print( "It's a tie!" )

# ========================================================
#Round3

computer = choice( OPTIONS )
human = input( "What is your play? " ).replace(" ","").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')\
   or ( computer=='M' and human=='P')\
   or ( computer=='M' and human=='R')\
   or ( computer=='S' and human=='M'):
    
    print( "Congrats, you win!" )
    humanScore = humanScore + 1
       
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P' )\
   or ( computer=='M' and human=='S')\
   or ( computer=='R' and human=='M')\
   or ( computer=='P' and human=='M'):
    print( "Sorry, you lose this time!" )
    computerScore = computerScore + 1
else:
    print( "It's a tie!" )

# ========================================================
# Outcome
if  computerScore > humanScore:
    print( "You lose!  I am sorry, but you are no match for me!" )
elif computerScore < humanScore:
    print( "You win the 3 rounds!  Congrats!" )
else:
    print( "Your 3 rounds end in a tie!" )