Question: 1 . Create a file called thermostat.py , and in the file create a class called Your next task is to implement a function for
Create a file called thermostat.py and in the file create a class called Your next task is to implement a function for this class called addschedule. Other than the self parameter, this function should take a time string and a temperature float as parameters. It should store the timetemperature pair where time is the key and temperature is the value in the schedules dictionary. If the time already exists, its value will be overwritten by the new temperature; otherwise it will create a new entry. You can assume the time string always represents a valid time. After this function is implemented, you can add schedules to your Thermostat object by for instance:
dev Thermostat
dev.addschedule:
dev.addschedule:
dev.addschedule:
dev.addschedule:
Note that the user doesnt need to add schedules in increasingascending order of time the schedules can be added in any arbitrary ordering of times.
Hint: Do NOT ask the user for any input in the function.. Your first task is to implement its constructor : other than the self parameter, it should take a number value as the default temperature parameter this parameter should be given a default value of so in the case the user doesnt provide this parameter, the default temperature will be
In the constructor you should create two instance variables attributes: one to store the default temperature, and another to store the schedules like described above. The most straightforward data type for schedules is a dictionary, as you can use it to store timetemperature pairs. So in the constructor, you should assign it to an empty dictionary indicating there is no schedule yet. Once you have the constructor implemented, you can create objects of the Thermostat class by using, for instance: Thermostat or dev Thermostat
Hint: Do NOT ask the user for any input in the function.
Ryanne Rusty,A
res': 'average':
Tip: this function can be easily implemented using list comprehension.
Your next task is to implement a function for this class called addschedule. Other than the self parameter, this function should take a time string and a temperature float as parameters. It should store the timetemperature pair where time is the key and temperature is the value in the schedules dictionary. If the time already exists, its value will be overwritten by the new temperature; otherwise it will create a new entry. You can assume the time string always represents a valid time. After this function is implemented, you can add schedules to your Thermostat object by for instance:
dev Thermostat
dev.addschedule:
dev.addschedule:
dev.addschedule:
dev.addschedule:
Note that the user doesnt need to add schedules in increasingascending order of time the schedules can be added in any arbitrary ordering of times.
Hint: Do NOT ask the user for any input in the function.
Now you will implement the special function str which casts this object to a string. Specifically, this function should return a string note: return, NOT print formatted as the following:
Default temperature: defaulttemp degrees
TIMEVALUE degrees
TIMEVALUE degrees
where defaulttemp is the actual value of the default temperature, and following from that are the timetemperature pairs sorted in increasingascending order of time, one on each line. Because the string should contain multiple lines, at the end of each line there must be a newline character
except the last line, which should NOT have a trailing newline character.
You dont need to implement the sorting algorithm yourself: Python provides a sorted function, which can take a dictionary and returns a list that contains the keys sorted in ascending order. For example, in our case, sortedselfschedules returns a list of times sorted in ascending order.
When you are done, test this function by calling printThermostat it should print out just one line:
Default temperature:
which is what it should look like when no schedule is in the thermostat yet. Then, call printdev where dev has schedules as seen in Section above. This should print out lines:
Default temperature: degrees
: degrees
: degrees
: degrees
: degrees
Hint: Do NOT ask the user for any input in the function.
Your last task is to implement a function called gettargettemperature. It should take an arbitrary query time string as the parameter, and it should return the target temperature at that query time. Imagine we sort the schedules in ascending order of time: TIME TIME TIME and so on This divides the entire timeline of a day : to : into intervals. If a query time is earlier than TIMEIE the earliest time in schedles this function should return the default temperature value if the quert time is after the last time it should returnthe temperature set at that last time.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
