Difference between revisions of "CSC111 Homework 4 Solutions 2014"
(Created page with "--~~~~ ---- =Problem 1= ==Version 1== <source lang="python"> #hw4a #Nicole Wong (bw) #Anna Render (bp) #(slightly edited by D. Thiebaut) # # Rock, Paper, Scissors game # The p...") |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:33, 6 March 2014 (EST) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:33, 6 March 2014 (EST) | ||
---- | ---- | ||
+ | <onlydft> | ||
+ | |||
=Problem 1= | =Problem 1= | ||
+ | |||
==Version 1== | ==Version 1== | ||
<source lang="python"> | <source lang="python"> | ||
Line 79: | Line 82: | ||
</source> | </source> | ||
+ | ==Version 2== | ||
+ | Another very good version: | ||
+ | <source lang="python"> | ||
+ | # 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!") | ||
+ | |||
+ | </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> | ||
+ | |||
+ | =Problem 3 (Optional)= | ||
+ | ==Version 1== | ||
+ | This version by Emily uses more advanced features than we have seen so far, but it's a good introduction to what we'll be able to do soon... | ||
+ | |||
+ | <source lang="python"> | ||
+ | # hw4c.py | ||
+ | # Emily Tizard (cn) | ||
+ | # Extra Credit problem | ||
+ | # | ||
+ | # user will provide name of a city (Boston, NY, or Montreal) | ||
+ | # followed by an integer number of degrees (temp) | ||
+ | # which the program will average for each city and print. | ||
+ | # program will stop prompting when user enters 'end' | ||
+ | |||
+ | |||
+ | |||
+ | # lists in which temperature data will be stored | ||
+ | bTemps = [ ] | ||
+ | nyTemps = [ ] | ||
+ | mTemps = [ ] | ||
+ | |||
+ | # prompting the user for weather report | ||
+ | |||
+ | weather = Input( "Enter a city (Boston, New York, or Montreal) \ | ||
+ | followed by an integer temperature in degrees F, or 'end' to stop: " ).upper() | ||
+ | |||
+ | #while the user has not ended the program | ||
+ | while weather != "END": | ||
+ | |||
+ | if weather.find("BOSTON") != -1: | ||
+ | nWeather = weather.replace( "BOSTON", ' ', 1 ) | ||
+ | nnWeather = int( nWeather.strip() ) | ||
+ | bTemps.append( nnWeather ) # to add the weather data to list | ||
+ | |||
+ | elif weather.find( "NEW YORK" ) != -1: | ||
+ | nWeather =weather.replace( "NEW YORK", ' ', 1 ) | ||
+ | nnWeather = int( nWeather.strip() ) | ||
+ | nyTemps.append( nnWeather ) | ||
+ | |||
+ | elif weather.find( "MONTREAL" ) != -1: | ||
+ | nWeather =weather.replace( "MONTREAL", ' ', 1 ) | ||
+ | nnWeather = int( nWeather.strip() ) | ||
+ | mTemps.append( nnWeather ) | ||
+ | |||
+ | weather = Input( "Any more weather data?" ).upper() | ||
+ | |||
+ | # to average the weather data | ||
+ | # and print it to the user | ||
+ | |||
+ | if len( bTemps ) > 0: | ||
+ | print( "The average temperature in Boston is: %.2f " % ( sum( bTemps )/ len( bTemps ) ) ) | ||
+ | if len( nyTemps ) > 0: | ||
+ | print( "The average temperature in New York is: %.2f" % ( sum( nyTemps ) / len( nyTemps ) ) ) | ||
+ | if len( mTemps ) > 0: | ||
+ | print( "The average temperature in Montreal is: %.2f" % ( sum( mTemps ) / len( mTemps ) ) ) | ||
+ | |||
+ | |||
+ | </source> | ||
+ | ==Version 2== | ||
+ | This version uses all the features we've seen so far. | ||
+ | <source lang="python"> | ||
+ | # hw4c.py | ||
+ | # Erika Earley (ar) | ||
+ | # 2/25/14 | ||
+ | # Problem 3: gets input from keyboard about a city and temperature | ||
+ | # and output average temeperature in each city | ||
+ | |||
+ | # define constant strings used in the program | ||
+ | promptText = "Please enter temperature recording for Boston, New York or Montreal. Enter the word 'end' to stop." | ||
+ | sentinel = "end" | ||
+ | user = "" | ||
+ | |||
+ | # (X)sumtemps is the sum of all the temperatures entered for a given city | ||
+ | # (X)numTemps is the number of entries given for a particular city | ||
+ | BsumTemps = 0 | ||
+ | BnumTemps = 0 | ||
+ | MsumTemps = 0 | ||
+ | MnumTemps = 0 | ||
+ | NYsumTemps = 0 | ||
+ | NYnumTemps = 0 | ||
+ | |||
+ | print(promptText) | ||
+ | while user != sentinel : | ||
+ | user = Input('> ') | ||
+ | if user != sentinel: | ||
+ | user = user.lower().strip() | ||
+ | if user.find('boston') != -1: | ||
+ | Btemp = user.replace('boston','') | ||
+ | BsumTemps = BsumTemps + int(Btemp) | ||
+ | BnumTemps = BnumTemps + 1 | ||
+ | |||
+ | elif user.find('montreal') != -1: | ||
+ | Mtemp = user.replace('montreal','') | ||
+ | MsumTemps = MsumTemps + int(Mtemp) | ||
+ | MnumTemps = MnumTemps + 1 | ||
+ | |||
+ | elif user.find('new york') != -1: | ||
+ | NYtemp = user.replace('new york','') | ||
+ | NYsumTemps = NYsumTemps + int(NYtemp) | ||
+ | NYnumTemps = NYnumTemps + 1 | ||
+ | |||
+ | # if the user did not reference a particular city at all, output | ||
+ | # 'none entered' for that city. | ||
+ | # compute and display average temperature for each city (allow 2 decimal places) | ||
+ | |||
+ | text = "Average temperature for" | ||
+ | noEntry = "None Entered" | ||
+ | |||
+ | if BnumTemps != 0: | ||
+ | bostonAvg = BsumTemps / BnumTemps | ||
+ | print("%s %s: %.2f" % (text,"Boston",bostonAvg)) | ||
+ | else: | ||
+ | print("%s %s: %s" % (text,"Boston",noEntry)) | ||
+ | if MnumTemps != 0: | ||
+ | montrealAvg = MsumTemps / MnumTemps | ||
+ | print("%s %s: %.2f" % (text,"Montreal",montrealAvg)) | ||
+ | else: | ||
+ | print("%s %s: %s" % (text,"Montreal",noEntry)) | ||
+ | if NYnumTemps != 0: | ||
+ | newyorkAvg = NYsumTemps / NYnumTemps | ||
+ | print("%s %s: %.2f" % (text,"New York",newyorkAvg)) | ||
+ | else: | ||
+ | print("%s %s: %s" % (text,"New York",noEntry)) | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | </onlydft> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Homework]] |