In L2 Problem 9 I have to confess I couldn't tget 2 of them as below
temp = '32'
if temp > 85:
elif temp > 62:
print('Cold') answer Hot
temp = 120
if temp > 85:
elif temp > 100:
elif temp > 60:
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 :)
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.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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
Not the answer you are looking for? Search for more explanations.
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
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.
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.
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: