anonymous
  • anonymous
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
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
pls use a code pasting site
anonymous
  • anonymous
what data types are you using and comparing?
anonymous
  • anonymous
Its from the OCW edx course that has just started week1

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
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
MicroBot
  • MicroBot
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.
anonymous
  • anonymous
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?
anonymous
  • anonymous
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.
anonymous
  • anonymous
My fault for rushing.
MicroBot
  • MicroBot
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.
anonymous
  • anonymous
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!
anonymous
  • anonymous
I think that is a little bug there though, MicroBot
MicroBot
  • MicroBot
what i mean is that they made this question for us to understand the meaning of semantics bugs.
anonymous
  • anonymous
Probably
anonymous
  • anonymous
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.
MicroBot
  • MicroBot
yes , exactly .
anonymous
  • anonymous
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?
MicroBot
  • MicroBot
something like : if temp >60 and temp <=85: ...... elif temp>85 and temp<=100: ...... elif temp>100: ....... else: .......

Looking for something else?

Not the answer you are looking for? Search for more explanations.