A community for students.
Here's the question you clicked on:
 0 viewing
minimallinux
 3 years ago
In L2 Problem 9 I have to confess I couldn't tget 2 of them as below
temp = '32'
if temp > 85:
print('Hot')
elif temp > 62:
print('Comfortable')
else:
print('Cold') answer Hot
correct
temp = 120
if temp > 85:
print('Hot')
elif temp > 100:
print('REALLY HOT!')
elif temp > 60:
print('Comfortable')
else:
print('Cold') answer Hot
The answer 'Hot' was not expected, anyone explain why ? Thanks
minimallinux
 3 years ago
In L2 Problem 9 I have to confess I couldn't tget 2 of them as below temp = '32' if temp > 85: print('Hot') elif temp > 62: print('Comfortable') else: print('Cold') answer Hot correct temp = 120 if temp > 85: print('Hot') elif temp > 100: print('REALLY HOT!') elif temp > 60: print('Comfortable') else: print('Cold') answer Hot The answer 'Hot' was not expected, anyone explain why ? Thanks

This Question is Closed

bwCA
 3 years ago
Best ResponseYou've already chosen the best response.0pls use a code pasting site

bwCA
 3 years ago
Best ResponseYou've already chosen the best response.0what data types are you using and comparing?

minimallinux
 3 years ago
Best ResponseYou've already chosen the best response.0Its from the OCW edx course that has just started week1

jordibs
 3 years ago
Best ResponseYou've already chosen the best response.0The firdt question you do is if temp > 85. Because it's true the answer is "Hot". The correct sequence of nested ifthenelse is: If temp > 120. Then ready hot Else if temp > 100 then hot Else if temp > 60 then comfortable Else Cold

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1hint1: in the 1st question it gives the temp the value of a string. hint2: in the second question u have to think how if works. when it finds a statment that is True it lets it go inside and do the action block under it.After its done it exits if and continues his way ignoring the rest of the tests thet it might of not tested yet. forgive my bad english,if u dont understand it send me a mess and ill try to explain more.

Chris2332
 3 years ago
Best ResponseYou've already chosen the best response.0Right so... For the first one you are getting the answer of 'Hot 'cause you are comparing a string to a number. In Python v2.7.x any string (including the empty space) will be bigger than any number. For the second one, I can't see why the 'Hot' answer was not expected? You are checking if the temp (120) is larger than 85, which it is, therefore you are getting 'Hot'. The correct order of the ifelse structure is as you have in the first one, so you move from the bigger to the smaller! Does it make sense?

minimallinux
 3 years ago
Best ResponseYou've already chosen the best response.0Yes, got it now, the first one is trick I should've seen the string, second one I misjudged the temp to be set at 85 when it is 120, however another thing is that I didn't remember that it takes and prints the first statement if true regardless of a second statement also being true. Tricky stuff ! Thanks all.

minimallinux
 3 years ago
Best ResponseYou've already chosen the best response.0My fault for rushing.

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1q2: Basically the point is that u learn that python follows if as the programmer wrote it. Here the programmer wrote 1st test to be for >85 and then for >100. So if the tester brings in a number that would give both the value of True , python chooses the 1st in order as answer. > means when u write ur code think of possible answers users might give. Correct me if im wrong: this must be one example of semantics bug.

Chris2332
 3 years ago
Best ResponseYou've already chosen the best response.0Well... that's the idea of the ifelse construct. As soon as it satisfies a condition, it will execute the statements below it ignoring the rest!

minimallinux
 3 years ago
Best ResponseYou've already chosen the best response.0I think that is a little bug there though, MicroBot

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1what i mean is that they made this question for us to understand the meaning of semantics bugs.

minimallinux
 3 years ago
Best ResponseYou've already chosen the best response.0It shows that there are 2 ways of interpreting the statement only one is right, for eg it 120 > 85 print hot and if 120 > 100 print really hot, well you would say that its > 100 so really hot, but doesn't work like that, prints the first correct one in order it finds.

Chris2332
 3 years ago
Best ResponseYou've already chosen the best response.0Let's assume another scenario... if the program went for both of them, since both conditions are met, it would print 'Hot' and 'Really Hot'. Would that be something acceptable/desirable? How would you restrict such behavior?

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1something like : if temp >60 and temp <=85: ...... elif temp>85 and temp<=100: ...... elif temp>100: ....... else: .......
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.