Difference between revisions of "CSC111 Homework 3 Solution Programs 2014"

From dftwiki3
Jump to: navigation, search
(Version 2)
(Version 3)
Line 290: Line 290:
  
 
# Generate computer choices
 
# Generate computer choices
OPTIONS=['R','P','S','MUG']
 
 
 
computer=choice(OPTIONS)
 
computer=choice(OPTIONS)
  
Line 354: Line 352:
  
 
# Generate computer choices
 
# Generate computer choices
OPTIONS=['R','P','S','MUG']
 
 
 
computer=choice(OPTIONS)
 
computer=choice(OPTIONS)
  

Revision as of 10:56, 27 February 2014

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


Program 1


Version 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!" )


Version 2


Here we have a solution where a while loop is used. Notice how much tighter the code is when we use loops! The code is well documented and comments highlights the different sections.

# hw3a.py
# Erika Earley (ar), Ayliffe Brown (cr)
# (slightly edited by D. Thiebaut)
# 2/18/14
# write a rock-paper-scissors-MUG game that includes new for rules for Mug
# choice, recognizes both lowercase and uppercase inputs from the user, lets
# the user play 3 rounds with the computer, keeps track of the score, and
# prints the result for each round and for each 3-round game.


from random import choice

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

# Create/initialize the variables which will keep track of the score
computerScore = 0
humanScore = 0

# =============================================
# Create/initialize varible which will keep track of # rounds
# After 3 rounds, the result of the entire game will be printed
round = 0
while round < NOROUNDS:

    computer = choice( OPTIONS )
    human = input( "What is your choice?\n" ).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=='S' and human=='M' ) \
       or ( computer=='M' and human=='P' ) \
       or ( computer=='M' and human=='R' ) :
        print( "YOU WIN!" )
        humanScore = humanScore + 1
        
    elif ( computer=='P' and human=='R' ) \
       or ( computer=='S' and human=='P' ) \
       or ( computer=='R' and human=='S' ) \
       or ( computer=='M' and human=='S' ) \
       or ( computer=='P' and human=='M' ) \
       or ( computer=='R' and human=='M' ) :
        print( "YOU LOSE" )
        computerScore = computerScore + 1
    else:
        print( "It's a tie!" )
    round = round + 1       

# =============================================
# Compare the score-count for the computer vs. the score-count for
# the human to determine the winner
if  computerScore > humanScore:
    print( "You lose the whole game! Boooo." )
elif computerScore < humanScore:
    print( "You win the 3 rounds! WOOHOO!" )
else:
    print( "Your 3 rounds end in a tie! How boring." )


Version 3


This version gets a prize for good documentation!

# Name of the program: hw3a.py
# Name of the programmers:Jing Zhang(cu), Jingyang Zhang (ak)
# Date of the program written: Feb. 13th 2014


# This program is a revision of the Rock, Paper, Scissor game,
# with a new option "mug" which has the following rule:
# Mug wins scissors, and fails to both paper and rock. 
# The program generates a computer's choice of the four options,
# and asks for an input from users.
# It will play 3 rounds and annouce who is the final winner. 



# Import
from random import choice


#---------------------Game With Three Rounds----------
# Initialize count for the number of time human wins and ties
ccount=0
hcount=0

#-------------------------Round One-------------------

# Generate computer choices
OPTIONS=['R','P','S','MUG']

computer=choice(OPTIONS)

# Ask for human input
human=(input("Please choose one from R, P, S, and MUG: ").strip()).upper()
print()


# Test the result
# 16 cases:
# H   C
#--- ---
#-------Ties
# R   R
# S   S
# P   P
# MUG MUG

#-------Cwin
# R   P
# P   S
# S   R
# S  MUG
# MUG R
# MUG P

#-------Hwin
# R   S
# R  MUG
# P   R
# P  MUG
# S   P
# MUG S



if human==computer:
    print("Tie!")
    print()

elif (human=='R' and computer=='P') or (human=='P' and computer=='S')\
    or (human=='S' and computer=='R') or(human=='S' and computer=='MUG')\
    or (human=='MUG' and computer=='R') or (human=='MUG' and computer=='P'):
    print("Computer Win!")
    print()
    ccount=ccount+1
elif (human=='R' and computer=='S') or (human=='R' and computer=='MUG')\
    or (human=='P' and computer=='R') or (human=='P' and computer=='MUG')\
    or (human=='S' and computer=='P') or (human=='MUG' and computer=='S'):
    print("You Win!")
    print()
    hcount=hcount+1
    
# Show computer's choice
print("The computer has chosen", computer)
print()



#-------------------------Round Two-------------------

# Generate computer choices
computer=choice(OPTIONS)

# Ask for human input
human=(input("Please choose one from R, P, S, and MUG: ").strip()).upper()
print()


# Test the result
# 16 cases:
# H   C
#--- ---
#-------Ties
# R   R
# S   S
# P   P
# MUG MUG

#-------Cwin
# R   P
# P   S
# S   R
# S  MUG
# MUG R
# MUG P

#-------Hwin
# R   S
# R  MUG
# P   R
# P  MUG
# S   P
# MUG S



if human==computer:
    print("Tie!")
    print()

elif (human=='R' and computer=='P') or (human=='P' and computer=='S')\
    or (human=='S' and computer=='R') or(human=='S' and computer=='MUG')\
    or (human=='MUG' and computer=='R') or (human=='MUG' and computer=='P'):
    print("Computer Win!")
    print()
    ccount=ccount+1
elif (human=='R' and computer=='S') or (human=='R' and computer=='MUG')\
    or (human=='P' and computer=='R') or (human=='P' and computer=='MUG')\
    or (human=='S' and computer=='P') or (human=='MUG' and computer=='S'):
    print("You Win!")
    print()
    hcount=hcount+1
    
# Show computer's choice
print("The computer has chosen", computer)
print()




#-------------------------Round Three-----------------

# Generate computer choices
computer=choice(OPTIONS)

# Ask for human input
human=(input("Please choose one from R, P, S, and MUG: ").strip()).upper()
print()


# Test the result
# 16 cases:
# H   C
#--- ---
#-------Ties
# R   R
# S   S
# P   P
# MUG MUG

#-------Cwin
# R   P
# P   S
# S   R
# S  MUG
# MUG R
# MUG P

#-------Hwin
# R   S
# R  MUG
# P   R
# P  MUG
# S   P
# MUG S



if human==computer:
    print("Tie!")
    print()

elif (human=='R' and computer=='P') or (human=='P' and computer=='S')\
    or (human=='S' and computer=='R') or(human=='S' and computer=='MUG')\
    or (human=='MUG' and computer=='R') or (human=='MUG' and computer=='P'):
    print("Computer Win!")
    print()
    ccount=ccount+1
elif (human=='R' and computer=='S') or (human=='R' and computer=='MUG')\
    or (human=='P' and computer=='R') or (human=='P' and computer=='MUG')\
    or (human=='S' and computer=='P') or (human=='MUG' and computer=='S'):
    print("You Win!")
    print()
    hcount=hcount+1
    
# Show computer's choice
print("The computer has chosen", computer)
print()


#-------------------------Game Result------------------
if hcount==ccount:
    print("It is a Tie!")
elif hcount<ccount:
    print("Sorry, computer wins",ccount-hcount,"times more than you!")
else:
    print("Congrats! You win",hcount-ccount,"times more than the computer!")


Program 2


The program below uses a while loop. You didn't have to. But it provides a nice solution now that we know how to test conditions for looping. It is well documented, and comments in the code highlights what different parts of the code are doing.

# hw3b.py
# Yijin Wei (bd) and Julieanna Niu (bi)
# (slightly edited by D. Thiebaut)
# This program will play three rounds of rock-paper-scissors with the user and
# display the result of the game. The input can be r, s, or p in lower or upper case.
# If the user enters an invalid input, the program will have one chance to input
# a new choice, otherwise the program assigns one randomly.

from random import choice

OPTIONS      = [ 'R', 'P', 'S' ]
USERWINS     = "You win!"
COMPUTERWINS = "The computer wins!"
NOROUNDS     = 3

totalTime = 0
userScore = 0
computerScore = 0

# ============================================
# repeats a round while the total amount of rounds is less than three
while totalTime < NOROUNDS:

    computer = choice( OPTIONS )
    print("The computer has chosen",computer )
    human = input( "Your play? " ).upper()

   # asks the user for new character if the user input is invalid the first time
    if human != 'R' and human != 'P' and human != 'S':
       human = input ("Please enter a valid character: ").upper()

      # assigns a character if the user input is invalid the second time
       if human != 'R' and human != 'P' and human != 'S':
         human=choice( OPTIONS )
         print("The computer has assigned you",human)
    print( "Your play: %s  Computer Play: %s" % ( human, computer ) )
    
   # the program runs the series of test cases 
    if human==computer:
        print( "It's a tie!" )
    elif human == 'P' and computer =='R' \
         or human == 'R' and computer =='S' \
         or human == 'S' and computer =='P':
        print( USERWINS )
        userScore = userScore+1
    else:
        print( COMPUTERWINS )
        computerScore = computerScore +1
    totalTime=totalTime+1
    print()

# ============================================
# determines and outputs the final result

if userScore>computerScore:
    print ("You are the winner of the three rounds!")
elif computerScore>userScore:
    print("The computer is the winner of the three rounds!")
else:
    print("The overall result is a tie!")


Program 3


Version 1


This version uses counts to figure out the difference between "mother" by itself and "Mother Theresa." It works!

# hw3c.py
# Stephanie Defrank (am) and Elizabeth Jarrett (an)
# (Edited by D. Thiebaut)
# Description: Gets a line of input from the user.  If the keywords "mother" or "father"
# are found in the input, then the program outputs a sentence indicating that it has recognized
# a family topic.  But if the user enters "Mother Theresa" or "Father Christmas" in the input, without
# any family member mentioned, then the program will treat the input as general comment.

# Greet user and get an input
answer = input( "How was your day? " ).lower()

# figure out how to respond
if answer.find ( "mother" ) !=-1 \
    or answer.find ("father") !=-1 \
    or answer.find( "mother" )!= -1 and answer.find("Theresa") !=-1 \
    or answer.find( "father" ) != -1 and answer.find("Christmas") !=-1 \
    or answer.find("sister") != -1 or answer.find("brother") !=-1 :

        print( "I would like to hear more about your family?" )
else:
        print( "Glad to hear that!" )


Version 2


This version is more elegant. It simply replaces "Mother Theresa" and "Father Christmas" by null strings, in effect removing them from the testing. This program was especially well documented.

# Name of the program: hw3c.py
# Name of the programmers:Jing Zhang( ), Jingyang Zhang (ak)
# Date of the program written: Feb. 13th 2014

# This program detects whether users have family issues according to 
# Their answers. If the user mentions mother, father, sister, or brother in
# the answer, the program will suggest to talk about the family. Otherwise,
# the program will reply a generic answer. 


# Ask for user's input
answer=input("How was your day?").lower()

# Remove Mother Theresa and/or Father Christmas from the answer if the
# user does enter one or both of them

answerupdates=answer.replace( 'mother theresa', '' )
answerupdates=answerupdates.replace( 'father christmas', '' )
    
# Check the updated answer to see if still mentions family members
if answerupdates.find( "mother" ) != -1 or answerupdates.find( "father" ) != -1\
   or answerupdates.find( "brother" ) != -1\
   or answerupdates.find( "sister" ) != -1:
    print( "I would like to hear more about your family." )
else:
    print( "Glad to hear that!" )