 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Final Exam Review Fall 2003: Random Weather Comments
Questions, comments, answers?
Back to Fall2003 Final Exam Review
import random
def sentence(x):
under = ["Watch out for ice!", "Did I move North?!?"]
middle = ["I can't wait for winter to be over!" , "Come on, Spring!" ]
between = ["It's getting warmer!" , "REMOVEDght jacket or less weather." ]
above = ["FINALLY! REMOVEDmmer!" , "Time to go swimming!" ]
if x < 32:
print random.choice(under)
if x > 32 and x < 50:
print random.choice(middle)
if x > 50 and x < 80:
print random.choice(between)
if x > 80:
print random.choice(above)
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!","REMOVEDght jacket or less weather."]
hot=["FINALLY, REMOVEDmmer!","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)
For some reason, some of the "less than" signs have decided not to come out and play.
Aren't the above programs missing some = signs? REMOVEDke if x >=32 and x=50?
wouldn't it be x=50? because that doesn't stop anything over 32
i meant x=50
my less than isn't showing up!
| Again, read the FAQ. < won't show up as-is. It gets interpreted as the beginning of an HTML tag. You have to type <. Mark Guzdial |
I found the problem a little vague...I wasn't sure, for example, if the temperature is exactly 50, which comments should be printed. Should I use <= 50 or just <50 for the second set of comments? Will y'all be that picky?
You gave us two examples. Can you please explain how they are different, and which one we need to use, or do we have to combine them in some way? I noticed from the postings that people used the first example. Thank you.
| Yeah, that was one of the faux pas in this problem – I didn't explain what to do with the end points. My mistake – sorry! Mark Guzdial |
If I want to say between 32 and 50, what syntax do I use? I tried using "if temp >=32 and <50" but it gave me a syntax error.
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!" , "REMOVEDght jacket or less weather." ]
hot = ["FINALLY! REMOVEDmmer!", "Time to go swimming!" ]
if temp < 32:
print random.choice(cold)
if temp < 50:
print random.choice(warm)
if temp < 80:
print random.choice(warmer)
if x > 80:
print random.choice(hot)
| There are two wans to check for a range. if temp >= 32 and temp < 50 will work, but so will if 32 <= temp < 50. Mark Guzdial |
import random
def weathercomment(temperature):
Winter = ["Watch out for ice!" , "Did I move North?!?"]
Spring = ["I can't wait for winter to be over!" , "Come on, Spring!"]
AlmostREMOVEDmmer = ["It's getting warmer!" , "REMOVEDght jacket or less weather."]
REMOVEDmmer = ["FINALLY! REMOVEDmmer!" , "Time to go swimming!"]
if temperature < 32:
print random.choice(Winter)
if temperature >= 32 or temperature <= 50:
print random.choice(Spring)
if temperature >= 50 or temperature <= 80:
print random.choice(AlmostREMOVEDmmer)
if temperature > 80:
print random.choice(REMOVEDmmer)
I don't know why it's printing 2 random sentence. One from the list it suppose to base on the temperature given and the other one from the "Alomst" list
| Please read the FAQ page on how to format your code. Try some boundary testing. Input 80, 50, and 32 as a temperature. Is it also printing 2 random sentences? Student56 |
well, for 50 degrees you have it as spring and almost summer...and 32 degrees, it's missing a less than sign...
oh...and it's AND not OR
i am still confused about the = with the < or > because if you had, for example:
if temperature >=32 and temperature =50:
print random.choice(.....)
if temperature >=50 and temperature =80:
print random.choice(.....)
when you type in 50 wouldn't it give you one from each of those 2 groups because they both have something or equal to bounding them??
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!" , "REMOVEDght jacket or less weather." ]
hot = ["FINALLY! REMOVEDmmer!", "Time to go swimming!" ]
if temp < 32:
print random.choice(cold)
if temp < 50:
print random.choice(warm)
if temp < 80:
print random.choice(warmer)
if temp > 80:
print random.choice(hot)
doesn't this work? you eliminate a certain set of numbers in each round, so there is no need for = signs in the code. correct?
oops..i meant:
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!" , "REMOVEDght jacket or less weather." ]
hot = ["FINALLY! REMOVEDmmer!", "Time to go swimming!" ]
if temp < 32:
print random.choice(cold)
if temp < 50:
print random.choice(warm)
if temp < 80:
print random.choice(warmer)
if temp > 80:
print random.choice(hot)
| Without a return, each and every IF is executed and evaluated – there's no "elimination" going on. Instead, you'll get multiple things printing. (Try it!) Mark Guzdial |
Link to this Page