Question
Hi, I need help from this Python assignment: Let me know if you need anything from me and I will provided Implementing our Choose Units
Hi, I need help from this Python assignment:
Let me know if you need anything from me and I will provided
Implementing our Choose Units Function / Printing Some Statistics
Oh, this is truly exciting. We've now got data loaded into our current_set object, and we are ready to display that data!
First, in order to make our program internationally useful and also recognized by the broad scientific community, is to implement our choose_units() function and use our previously implemented convert_units() function.
def convert_units(celsius_value): """This function will ask for temperature in Celsius and convert to F or K depending on unit value: C has unit 0, F has unit 2 and K has unit 2 """ unit_int = int(unit) if unit_int == 1: fahrenheit_value = (celsius_value * 1.8) + 32 return fahrenheit_value if unit_int == 2: kelvin_value = celsius_value + 273.15 return kelvin_value
First we need to create a global integer variable (not a constant, be sure to use the right naming convention) to hold the current unit selection, and set a default value. Because I'm just crazy wild, I called my variable current_unit. The variable should have a default value that indicates units in Celsius.
We'll use a global constant dictionary to hold on to the valid units. Because dictionaries are old news to us, I'm giving it to you.
UNITS = { 0: ("Celsius", "C"), 1: ("Fahrenheit", "F"), 2: ("Kelvin", "K") }
Next we need to create a function to choose units. It should have the following functionality:
REPORT what the current units are
GIVE A MENU of the available units
ASK the user to choose a new unit. Do not leave until the user chooses correctly. Be sure to filter for non-integer values.
CHANGE current_units to the user's selection.
Important: You should use the dictionary above to generate the menu options and validate the user selection. PAY ATTENTION TO THIS!
When called, your function should generate something like this:
STEM Center Temperature Project Eric Reed Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit What is your choice? 2 Current units in Celsius Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin Which unit? 1 Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit What is your choice? 2 Current units in Fahrenheit Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin Which unit? 2 Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit What is your choice? 2 Current units in Kelvin Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin Which unit? 43 Please choose a unit from the list Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin Which unit? a *** Please enter a number only *** Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin Which unit? 0 Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit What is your choice?
Now, let's make sure you implemented this correctly. Add a an item to the dictionary so it looks like this WITHOUT MAKING ANY OTHER CHANGES TO YOUR CODE:
UNITS = { 0: ("Celsius", "C"), 1: ("Fahrenheit", "F"), 2: ("Kelvin", "K"), 5: ("Rankine", "R") }
Of course, this wouldn't work with our convert_units() function, but it's okay, it'll only be there for a second. Now make sure you can reproduce this, including the error entry of 4:
STEM Center Temperature Project Eric Reed Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit What is your choice? 2 Current units in Celsius Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin 5 - Rankine Which unit? 4 Please choose a unit from the list Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin 5 - Rankine Which unit? 5 Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit What is your choice? 2 Current units in Rankine Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin 5 - Rankine Which unit?
Now make sure you take that Rankine line out.
Alright, got that. You can test out your convert_units() function if you like, and make sure it works
Now we need to implement the method get_avg_temperature_day_time(self, active_sensors, day, time). I listed out the parameters to remind you. We still want it to return None if there is no dataset loaded. Now we also want it to return None if there are no sensors active in active_sensors or if the active sensors have no readings (you can do both in one statement!). And if there are readings available, we want it to find all the samples based on our filter_list at a particular day of week and time of day, and return the average as a floating point number (also known as a float).
It's up to you how to implement this. I used a list comprehension to create a list of just the temperature field of each matching line, and then it was easy to calculate the average. I do encourage you to do a list comprehension. They are oh, so elegant, especially when we are filtering a few values like this, and they are one of the best known features of Python. Remember, this function must not print anything out.
Once you are done, let's check and make sure this is working for you. Add a line after your call to print_menu() like this:
print(current_set.get_avg_temperature_day_time(filter_list, 5, 7))
We don't need an if statement like we did for the last assignment. This time we're being good programmers and using an accessor. If we don't have a dataset loaded the accessor will return None, and the print function will dutifully print None as well!
After loading the dataset though, you should see the number 20.4554411...... pop out. That's the average of temperatures on day 5 (Friday) at hour 7 (7AM-8AM). Now go to edit room filter and remove "Out". When you go back to the main menu you should see 20.86392..... No sense continuing until you get these numbers! Now go back and remove ALL the rooms from the filter. The average temp should report None.
Leave the testing line in for your submission, please, then remove it before the next assignment.
We'll use the get_avg_temperature_day_time() function for the next assignment, but as long as we're on a roll with summary statistics let's implement menu option 4. This should be a piece of cake, it's very similar to what we just did.
First implement get_summary_statistics(self, active_sensors) to return a tuple of floats with the minimum, maximum and average temperature of the sensors that are active inactive_sensors. If there is no data, this method should return None. Remember, this function must not print anything out, that's the job of our helper function, print_summary_statistics()...
Well, just take a look at the sample run and you'll know what to do for print_summary_statistics(). Remember to change "None" to the appropriate object names in the menu routine . Be sure print_summary_statistics() behaves well in these situations:
Normal operation (all sensors active, and some sensors filtered)
No data loaded
All sensors off
Notice in the sample run that temperatures are rounded to two decimal places. Notice also that the units are listed! Those should come from the units dictionary, of course, not from some kind of if statement. Hint, in this case the units dictionary behaves "sort-of" like a multi-dimensional array, as described in the modules. That also means you need to use convert_units to get the right values. Finally, note that the name we gave the dataset is printed out with the summary statistics!
The sample run is really long, don't be shy to make yours long as well! Put your program through its paces:
STEM Center Temperature Project Eric Reed Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit None What is your choice? 4 Please load data file and make sure at least one sensor is active Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit None What is your choice? 1 Please enter the filename of the new dataset: Temperatures2017-08-06.csv Loaded 11724 samples Please provide a 3 to 20 character name for the dataset Test Week Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit 20.45544117647059 What is your choice? 4 Summary statistics for Test Week Minimum Temperature: 16.55 C Maximum Temperature: 28.42 C Average Temperature: 21.47 C Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit 20.45544117647059 What is your choice? 2 Current units in Celsius Choose new units: 0 - Celsius 1 - Fahrenheit 2 - Kelvin Which unit? 1 Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit 20.45544117647059 What is your choice? 4 Summary statistics for Test Week Minimum Temperature: 61.79 F Maximum Temperature: 83.16 F Average Temperature: 70.64 F Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit 20.45544117647059 What is your choice? 3 4201: Foundations Lab [ACTIVE] 4204: CS Lab [ACTIVE] 4205: Tiled Room [ACTIVE] 4213: STEM Center [ACTIVE] 4218: Workshop Room [ACTIVE] Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end 4201 4201: Foundations Lab 4204: CS Lab [ACTIVE] 4205: Tiled Room [ACTIVE] 4213: STEM Center [ACTIVE] 4218: Workshop Room [ACTIVE] Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end 4204 4201: Foundations Lab 4204: CS Lab 4205: Tiled Room [ACTIVE] 4213: STEM Center [ACTIVE] 4218: Workshop Room [ACTIVE] Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end x Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit 19.910638297872342 What is your choice? 4 Summary statistics for Test Week Minimum Temperature: 61.79 F Maximum Temperature: 83.16 F Average Temperature: 70.13 F Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit 19.910638297872342 What is your choice? 3 4201: Foundations Lab 4204: CS Lab 4205: Tiled Room [ACTIVE] 4213: STEM Center [ACTIVE] 4218: Workshop Room [ACTIVE] Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end 4205 4201: Foundations Lab 4204: CS Lab 4205: Tiled Room 4213: STEM Center [ACTIVE] 4218: Workshop Room [ACTIVE] Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end 4213 4201: Foundations Lab 4204: CS Lab 4205: Tiled Room 4213: STEM Center 4218: Workshop Room [ACTIVE] Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end 4218 4201: Foundations Lab 4204: CS Lab 4205: Tiled Room 4213: STEM Center 4218: Workshop Room Out: Outside [ACTIVE] Type the sensor number to toggle (e.g.4201) or x to end Out 4201: Foundations Lab 4204: CS Lab 4205: Tiled Room 4213: STEM Center 4218: Workshop Room Out: Outside Type the sensor number to toggle (e.g.4201) or x to end x Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit None What is your choice? 4 Please load data file and make sure at least one sensor is active Main Menu --------- 1 - Process a new data file 2 - Choose units 3 - Edit room filter 4 - Show summary statistics 5 - Show temperature by date and time 6 - Show histogram of temperatures 7 - Quit None What is your choice? 7 Thank you for using the STEM Center Temperature Project >>>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started