Final Exam Review Spring 2004: Random Weather Comments
Link back to Sp2004 Final Exam Review
Questions and Answers?
import random
def weatherComment(temp):
freezing = ["Watch out for 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:
print random.choice(freezing)
if temp > 32 and temp < 50:
print random.choice(cold)
if temp > 50 and temp < 80:
print random.choice(warm)
if temp > 80:
print random.choice(hot)
My code for this problem works, but I don't get where in the problem you would put the equal signs to include the numbers 32, 50, and 80. Or do you just not include them because it says less than 32, and then between 32 and 50? Sana Yousufi
just do ">=" or "<=" ...that works
I know that's how you would do it, but I was wondering with which number it would go with according to the question. Sana Yousufi
This should be right Jasmina Pacariz
import random
def weathercoment(temperature):
if temperature 30:
phrase=["Watch out for ice!", "Did I move north?!?"]
if temperature > 32 and temperature 50:
phrase=["I can't wait for winter to be over!", "Come on, Spring!"]
if temperature >50 and temperature 80:
phrase=["It's getting warmer!", "Light jacket or less weather."]
if temperature >80:
phrase=["FINALLY!Summer!", "Time to go swimming!"]
print random.choice(phrase)
it's not
| Jasmina, do read the FAQ on how to post code so that we can read it. Thanks! Mark Guzdial |
here's my code (just half of the phrases bc I'm lazy):
import random
def weathercomment(temp):
phrases1 = ["Watch out for ice!", "Did I move North?"]
phrases2 = ["I can't wait for winter to be over!", "Come on, Spring!"]
if temp < 32:
return random.choice(phrases1)
if temp >=32 and temp < = 50:
return random.choice(phrases2)
My question is whether I should also include a "print" line along with the return line (like the first example), or just leave the return (like the second).Thank you, no more
the second to last line of my my code should read:
if temp >=32 and temp<=50:
thanks, no more
For some reason I can't get my "less-than" sign right... ? Pedro
i fixed it for you...
| The problem says "return". Think about using this in a homepage generator. Should it print? Mark Guzdial |
i'm not too clear on how return works...it returns the value and the value will be stored under the temp, right?(temp is the input variable)
Can someone explain why Jasmina's code isn't right? Yeah, the spacing is off, but it makes perfect sense to me! Thanks
Jasminas code is wrong because she needs to say things such as : "if temp => 32 and temp=50"..just saying what she said doesn't define the parameters clearly..understand?
A minor technicality, as written there is no statement that goes with the temperature = 80.
A question does it matter which symbol comes first in equal to or greater than expressions i.e. => vs. >= ?
| In most languages, it needs to be written like this ">=", but Python is pretty flexible – => might work, too. Mark Guzdial |
import random
def weathercomment(x):
freezing=["Watch out for 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 x 32:
print random.choice(freezing)
if x >= 32 and x = 50:
print random.choice(cold)
if x > 50 and x 80:
print random.choice(warm)
if x >= 80:
print random.choice(hot)
This works.
Prof. Guzdial, can you complete the sent. on the review where it says what each ex. in this part does. It says the 2nd one creates a randdom sentence "to be inserted into..." Into a webpage? Thanks.
I submit that return statements need to be added in place of print statements. You don't need them to print; you just need them to appear on the webpage. Jonathan Laing
do you use print random.choice(hot) or return random.choice(hot) ?
Nicole Maser
This works, so why would you need to "return"?
import random
def weathercomment(temp):
cold = ["Watch out for ice!", "Did I move North?!?"]
warm = ["I can't wait for winter to be over!" , "Come on, Spring!" ]
warmer = ["It's getting warmer!" , "Light jacket or less weather." ]
hot = ["FINALLY! Summer!", "Time to go swimming!" ]
if temp < 32:
print random.choice(cold)
if temp < 50 and temp >=32:
print random.choice(warm)
if temp < 80 and temp >=50:
print random.choice(warmer)
if temp >= 80:
print random.choice(hot)
Link to this Page