Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

minimallinux Group Title

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

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    pls use a code pasting site

    • 2 years ago
  2. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    what data types are you using and comparing?

    • 2 years ago
  3. minimallinux Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Its from the OCW edx course that has just started week1

    • 2 years ago
  4. jordibs Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 2 years ago
  5. MicroBot Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  6. Chris2332 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • 2 years ago
  7. minimallinux Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • 2 years ago
  8. minimallinux Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    My fault for rushing.

    • 2 years ago
  9. MicroBot Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  10. Chris2332 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

    • 2 years ago
  11. minimallinux Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I think that is a little bug there though, MicroBot

    • 2 years ago
  12. MicroBot Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    what i mean is that they made this question for us to understand the meaning of semantics bugs.

    • 2 years ago
  13. minimallinux Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Probably

    • 2 years ago
  14. minimallinux Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • 2 years ago
  15. MicroBot Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    yes , exactly .

    • 2 years ago
  16. Chris2332 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • 2 years ago
  17. MicroBot Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    something like : if temp >60 and temp <=85: ...... elif temp>85 and temp<=100: ...... elif temp>100: ....... else: .......

    • 2 years ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.