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
pls use a code pasting site
what data types are you using and comparing?
Its from the OCW edx course that has just started week1
The firdt question you do is if temp > 85. Because it's true the answer is "Hot". The correct sequence of nested if-then-else is: If temp > 120. Then ready hot Else if temp > 100 then hot Else if temp > 60 then comfortable Else Cold
hint1: 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.
Right 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 if-else structure is as you have in the first one, so you move from the bigger to the smaller! Does it make sense?
Yes, 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.
My fault for rushing.
q2: 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.
Well... that's the idea of the if-else construct. As soon as it satisfies a condition, it will execute the statements below it ignoring the rest!
I think that is a little bug there though, MicroBot
what i mean is that they made this question for us to understand the meaning of semantics bugs.
Probably
It 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.
yes , exactly .
Let'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?
something like : if temp >60 and temp <=85: ...... elif temp>85 and temp<=100: ...... elif temp>100: ....... else: .......
Join our real-time social learning platform and learn together with your friends!