Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Module help
#1
You have a thermostat that allows you to set the room to any temperature between 40 and 89 degrees.

The thermostat can be adjusted by turning a circular dial. For instance, if the temperature is set to 50 degrees and you turn the dial 10 clicks toward the left, you will set the temperature to 40 degrees. But if you keep turning 1 click to the left (represented as -1) it will circle back around to 89 degrees. If you are at 40 degrees and turn to the right by one click, you will get 41 degrees. As you continue to turn to the right, the temperature goes up, and the temperature gets closer and closer to 89 degrees. But as soon as you complete one full rotation (50 clicks), the temperature cycles back around to 40 degrees.

Write a program that calculates the temperature based on how much the dial has been turned. The number of clicks (from the starting point of 40 degrees) is contained in a variable. You should print the current temperature for each given click variable so that your output is as follows:

The temperature is 40
The temperature is 89
The temperature is 64
The temperature is 41
The temperature is 89
The temperature is 40


Below is my submission:

#For each click variable, calculate the temperature and print it as shown in the instructions

click_1 = 0
# TODO calculate the temperature, and report it back to the user

click_2 = 49
# TODO calculate the temperature, and report it back to the user

click_3 = 74
# TODO calculate the temperature, and report it back to the user

click_4 = 51
# TODO calculate the temperature, and report it back to the user

click_5 = -1
# TODO calculate the temperature, and report it back to the user

click_6 = 200
# TODO calculate the temperature, and report it back to the user

total_clicks = ['0' , '49' , '74' , '51' , '-1' , '200']

for count in total_clicks:
    
    count = int(count)
    
    count = count + 40 % 89
        
    print("The temperature is" ,count,)
However, the error I keep getting back is:

✖ ︎You should have printed this:


Output:
The temperature is 40 The temperature is 89 The temperature is 64 The temperature is 41 The temperature is 89 The temperature is 40
But you actually printed this:


The temperature is 40
The temperature is 89
The temperature is 114
The temperature is 91
The temperature is 39
The temperature is 240
Reply
#2
show your code so far.
We will be glad to help, but will not write it for you.
Reply
#3
I have my submission in the thread...
Reply
#4
You need to spend a bit of time and read forum rules.
I added BBCODE tags to your original post, this makes code, output and errors stand out
Also, please post actual error traceback verbatim, in error tags.
Reply
#5
It's an order of operations error. The % operator evaluates with multiplication and division, as it is based on division. So 40 % 89 is calculated first, equaling 40, and then 40 is added to count. You need parens around the addition to force the order to what you want (doing the modulus after the addition).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
I put parenthesis around the (40 % 89) but I am still getting the same output?
Reply
#7
That forces the 40 % 89 to be calculated first. That's already being calculated first due to the order of operations (Please excuse my dear aunt Sally?). Put the parens around the addition, so that it gets calculated first.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
I don't understand where else to put parenthesis other than:

count = count + (40 % 89)
Reply
#9
There are three ways to place the parentheses around one or more operators:

count = (count + 40) % 83
count = (count + 40 % 83)
count = count + (40 % 83)
Which one is "around the addition"? Which one makes your program work?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
Prompt:
You have a thermostat that allows you to set the room to any temperature between 40 and 89 degrees.

The thermostat can be adjusted by turning a circular dial. For instance, if the temperature is set to 50 degrees and you turn the dial 10 clicks toward the left, you will set the temperature to 40 degrees. But if you keep turning 1 click to the left (represented as -1) it will circle back around to 89 degrees. If you are at 40 degrees and turn to the right by one click, you will get 41 degrees. As you continue to turn to the right, the temperature goes up, and the temperature gets closer and closer to 89 degrees. But as soon as you complete one full rotation (50 clicks), the temperature cycles back around to 40 degrees.

Write a program that calculates the temperature based on how much the dial has been turned. The number of clicks (from the starting point of 40 degrees) is contained in a variable. You should print the current temperature for each given click variable so that your output is as follows:

The temperature is 40
The temperature is 89
The temperature is 64
The temperature is 41
The temperature is 89
The temperature is 40


I have submitted:

total_clicks = ['0' , '49' , '74' , '51' , '-1' , '200']
 
for count in total_clicks:
     
    count = int(count)
     
    count = (count + 40) % 89
         
    print("The temperature is" ,count,)
Results:
Output:
The temperature is 40 The temperature is 0 The temperature is 25 The temperature is 2 The temperature is 39 The temperature is 62
I have changed the location of the parenthesis to multiple places however still get incorrect numbers ran in the output...

Any help is appreciated!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020