Final Exam Review Fall 2004: Random Weather Comments
What are we supposed to do for the second part?
What are we supposed to do for the second part?
| What second part? There's only one question there to be answered. Kelly Lyons |
| Only 1 part. "Write a function named weathercomment that will take a temperature as input and return one of the phrases randomly. In other words, you want to use the temperature to decide which are the relevant phrases, then pick one randomly from there." Use the 2 functions to help you write a function named weathercomment. Angela Liang |
import random
def weathercomment(temp):
winter=["Watch out for ice!","Did I move North?!?"]
cold=["I can't wait for winter to be over!","Come on,Spring!"]
warmer=["It's getting warmer!","Light jacket or less weather."]
summer=["FINALLY! Summer!","Time to go swimming!"]
if temp<32:
random.choice(winter)
return random.choice(winter)
if temp>=32 and temp<=50:
random.choice(cold)
return random.choice(cold)
if temp>50 and temp<80:
random.choice(warmer)
return random.choice(warmer)
if temp>=80:
random.choice(summer)
return random.choice(summer)
How come you do not add the "print" statement at the beginning of line 8 and the other "random.choice(x)" statements?
| You do not add print because the directions say to return the phrase, not to print it. Kelly Lyons |
i'm not sure, but i don't think you need it. the random.choice's need to be returned in order to use them in a webpage and when you do "return", it also prints them
| Return does not print the phrase. The reason you are seeing it printed is becuase when you do not save the return value in some variable, or input it into another function, it gets returned to the screen. Kelly Lyons |
import random
def weatherPhrases(temp):
freezeWord = ["Did I move north?”, “Watch out for Ice!"]
chillWord = ["I can't wait for winter to be over!","Come on, Spring!"]
warmWord = ["It's getting warmer!" , "Light jacket or less weather."]
hotWord = ["FINALLY! Summer!" , "Time to go swimming!"]
if temp <=32:
print random.choice(freezeWord)
if 32 < temp <= 50:
print random.choice(chillWord)
if 50 < temp <=80:
print random.choice(warmWord)
if temp > 80:
print random.choice(hotWord)
Thanks Blake.
def weatherPhrases(temp):
freezeWord=["Did I move north?", "Watch out for Ice!"]
chillWord=["I can't wait for winter to be over!","Come on, Spring!"]
warmWord=["It's getting warmer!" , "Light jacket or less weather."]
hotWord=["FINALLY! Summer!" , "Time to go swimming!"]
if temp <=32:
return random.choice(freezeWord)
if 32<temp<=50:
return random.choice(chillword)
if 50<temp<=80:
return random.choice(warmWord)
if temp>80:
return random.choice(hotWord)
...AND, FINALLY, MARK, YOU SEEM TO BE ENJOYING SUMMER MORE THAN YOU WOULD LIKE TO ENJOY WINTER, WHY IS THAT SO? WINTER IS BETTER, YOU CAN ENJOY CHRISTMAS AND NEW YEAR's!
import random
def weathercomment(temp):
freeze=["Watch out for the ice!", "Did I move North?!?"]
cold=["I can't wait for winter to be over!", "Come on Spring!"]
warm=["It's getting warmer!", "Light jacket or less weather."]
hot=["FINALLY! Summer!", "Time to go swimming!"]
if temp<32:
return random.choice(freeze)
if temp>=32 and temp <=50:
return random.choice(cold)
if temp>50 and temp<80:
return random.choice(warm)
if temp>=80:
return random.choice(hot)
| Looks good... When doing this problem, make sure you have your less than, less than or equal to, greater than, and greater than or equal to signs correct. Angela Liang |
im a little confused as to when less than or less than or equal to should be used in this problem??...does between mean both values are inculded or not?
| The question is a bit vague on that. I would interpret it to be strictly less than 32; between 32 and 50 inclusive, between 50 and 80 exclusive, and greater than or equal to 80, but I could be wrong. If such confusions shows up on the exam, don't hesitate to ask and clarify before you start answering. Kelly Lyons |
Looks about right. Still a little vague about why Guzdial likes summer but not winter. Victor Du
Link to this Page