Chapter 4: Applying your knowledge

Thermostat program

face Josiah Wang

Here is the last challenge. Something less Math-y.

Write a simple Python thermostat program that reads in the current temperature as a floating point number (float) from the user. The ideal temperature is between 20 to 22 degree Celsius (inclusive). Depending on the temperature entered by the user, the program should print out either Too cold if the temperature is less than 20 degree Celsius, Too hot if it is more than 22 degree Celsius, or Just nice if it is between 20 and 22 degree Celsius (inclusive). It should also reduce the temperature by 1 degree Celsius if it is too hot, and increase the temperature by 1 degree Celsius if it is too cold. The program should end by printing out the adjusted temperature (or the same temperature if the temperature is ideal).

Hint: Use float() to convert a user input string into a float.

Sample run #1

Please enter temperature: 19
Too cold
New temperature: 20.0

Sample run #2

Please enter temperature: 70.2
Too hot
New temperature: 69.2

Sample run #3

Please enter temperature: 22
Just nice
New temperature: 22.0