Difference between revisions of "CSC111 Homework 4 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Version 1)
(Version 2)
Line 134: Line 134:
 
         print ("Better luck next time!")
 
         print ("Better luck next time!")
  
 +
</source>
 +
=Problem #2=
 +
<source lang="python">
 +
# hw4b.py
 +
# Lujun Jian (bx)
 +
# 02/21/2014
 +
#
 +
# Magic Lily
 +
#
 +
# Assume that we have a put a magic lily in Paradise Pond. The magic lily grows
 +
# in surface every 24 hours. When we first put it in the pond, it is only 1
 +
# square inch in area. We approximate the size of the pond to be 31,416 square feet.
 +
# a program that uses a while loop and that outputs the number of days it will
 +
# take for the lily to cover the whole pond.
 +
 +
 +
# Initial size of magic lily(square inches)
 +
lilySize = 1
 +
 +
# Growth rate of magic lily
 +
growthRate= 2
 +
 +
# Pond size (square feet, given)
 +
pondSize= 31416
 +
 +
#Pond Size expressed in square inches (1 foot = 12 inches)
 +
pondSize= pondSize *12 * 12
 +
 +
# grow the lily until it covers the pond
 +
num=0
 +
while lilySize < pondSize :
 +
      num = num +1
 +
      lilySize = lilySize*2
 +
     
 +
# display results and all relevant information
 +
print("Magic Lily")
 +
print("Initial size: 1 square inch")
 +
print("Growth rate: 2 times its size in 1 day" )
 +
print("Pond size: %d square feet" % pondSize )
 +
print("Minimum number of days required to cover the whole pond (or more): %d days" % num)
 
</source>
 
</source>

Revision as of 12:38, 6 March 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)

Version 2

Another very good version:

# hw4a.py
# Sarah Sutto-Plunz (bu)
# This progam evaluates a user's input to play the game of rock, paper, scissors.
# The computer randomly chooses one while the user is prompted to put an input.
# The program will keep asking for an input as long as it is not R,P, or S.
# The program keeps score of who is winning and prints it after each round.
# The program continues to run until there is a difference of 3 between the scores
# and then prints the final winner.

from random import choice
from random import seed
seed()

# declare score counters 
hcount = 0
ccount = 0

# constants used in the game
OPTIONS = [ 'R', 'P', 'S']
USERWINS = "You win!"
COMPUTERWINS = "I win!"

#--- while the score does not have a difference of three ---
while abs(ccount-hcount) <3:
    computer = choice( OPTIONS )
    human    = input( "Your play? " ).upper().strip()

    #--- while the human puts in an incorrect input ---
    while human != "R" and human != "S" and human != "P":
        print ("Invalid input, please enter a new character (R,P, or S): ")
        human    = input( "Your play? " ).upper().strip()

    #--- when the human enters the correct input ---
    else:
        print( "Your play: %s  Computer Play: %s" % ( human, computer ) )
        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 )
            hcount= hcount+1
        else:
            print( COMPUTERWINS )
            ccount = ccount +1

#--- announces the winner when there is a difference of 3 between scores---
if hcount> ccount:
        print ("You're a winner!")
else:
        print ("Better luck next time!")

Problem #2

# hw4b.py
# Lujun Jian (bx)
# 02/21/2014
#
# Magic Lily
#
# Assume that we have a put a magic lily in Paradise Pond. The magic lily grows
# in surface every 24 hours. When we first put it in the pond, it is only 1
# square inch in area. We approximate the size of the pond to be 31,416 square feet.
# a program that uses a while loop and that outputs the number of days it will
# take for the lily to cover the whole pond.


# Initial size of magic lily(square inches)
lilySize = 1

# Growth rate of magic lily
growthRate= 2

# Pond size (square feet, given)
pondSize= 31416

#Pond Size expressed in square inches (1 foot = 12 inches)
pondSize= pondSize *12 * 12

# grow the lily until it covers the pond
num=0
while lilySize < pondSize :
      num = num +1
      lilySize = lilySize*2
      
# display results and all relevant information
print("Magic Lily")
print("Initial size: 1 square inch")
print("Growth rate: 2 times its size in 1 day" )
print("Pond size: %d square feet" % pondSize )
print("Minimum number of days required to cover the whole pond (or more): %d days" % num)