In: Computer Science
Question
Objective:
The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to:
Discussion:
For this exercise you will be simulating a Weather Station responsible for recording hourly temperatures and reporting on certain characteristics (e.g., average temperature) from your recordings. You will also be expanding on your experience in creating functions and passing them more complex parameters (i.e., complete list arguments – sometimes empty and sometimes full).
Specifications:
DDI&T a Python program to input, store, and process hourly temperatures for each hour of the day (i.e., 24 temperatures). Your program should be divided logically into the following parts:
HourlyTemperatures = []
GetTemperatures(HourlyTemperatures)
This function must interactively prompt for and input temperatures for each of the 24 hours in a day (0 through 23). For each temperature that is input, verify that its value lies in the range of minus 50 degrees and plus 130 degrees (i.e., validate your input values). If any value is outside the acceptable range, ask the user to re-enter the value until it is within this range before going on to the next temperature (HINT: use a nested loop (e.g., Python’s equivalent to a do-while loop) that exits only when a value in the specified range has been entered). Store each validated temperature in its corresponding element of the list container passed as a parameter to the function (Hint: Look at the ‘append(…)’ function that can be called on a list container).
ComputeAverageTemp(HourlyTemperatures)
This function computes the average temperature of all the temperatures in the list and returns this average to the calling function.
DisplayTemperatures(HourlyTemperatures, AverageTemp)
which displays the values of your temperature list in a columnar format followed by the values for the high temperature, low temperature, and average temperature for the day. NOTE: If you want to create separate function(s) to find and return the high and low temperature values, then feel free to do so!
The resulting output should look something like this:
Hour Temperature
00:00 42
01:00 42
. . . . . . . . // Your program output must include all of these too!
22:00 46
23:00 48
High Temperature: 68
Low Temperature: 42
Average Temperature: 57.4
5. Since you have several created functions to perform each of the major steps of your solution, your main(): function should be quite simple. The pseudo code for main() might look something like this:
main()
#declare any necessary variable(s) and HourlyTemperatures list
while user-wants-to-process-another-days’-worth-of-temperatures
call GetTemperatures(HourlyTemperatures)
call ComputeAverageTemp(HourlyTemperatures)
call DisplayTemperatures(HourlyTemperatures, AverageTemperature)
ask if user want to process another days’ worth of temperatures
6. Test your program with at least two (2) different sets of temperatures. Make sure you enter values to adequately test your temperature-validation code (e.g., temperatures below –50 and above +130 degrees).
Deliverable(s):
Your deliverable should be a Word document with screenshots showing the sample code you have created, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them.
Turn in the properly documented source listing of your program and complete outputs from at least TWO (2) test “runs”. Additionally, turn in screen captures of some of your interactive inputs demonstrating that your program properly detects invalid inputs and prompts the user to re-enter the temperature(s).
Screenshot
Program
#Create a function to get a days each hours temperature
def GetTemperatures(HourlyTemperatures):
#Loop to get temperature 0-23
for i in range(0,24):
prompt="Enter
temperature in "
if(i<=9):
prompt+='0'+str(i)+":00 time in degrees: "
else:
prompt+=str(i)+":00 time in degrees: "
temp=int(input(prompt))
#Validity check
while(temp<-50 or
temp>130):
print('ERROR!!temperature should be -50 to 130
degrees.Re-enter')
prompt="Enter temperature in "
if(i<9):
prompt+='0'+str(i)+":00 time in degrees: "
else:
prompt+=str(i)+":00 time in degrees: "
temp=int(input(prompt))
#Append into list
HourlyTemperatures.append(temp)
#function to compute average of a day temperature
def ComputeAverageTemp(HourlyTemperatures):
avg=0
for i in range(0,len(HourlyTemperatures)):
avg+=HourlyTemperatures[i]
return avg/len(HourlyTemperatures)
#Function to display temperatire in column manner
#With high, low and average temperature
def DisplayTemperatures(HourlyTemperatures, AverageTemp):
highTemp=HourlyTemperatures[0]
lowTemp=HourlyTemperatures[0]
print('Hour
Temperature')
for i in range(0,len(HourlyTemperatures)):
if(highTemp<HourlyTemperatures[i]):
highTemp=HourlyTemperatures[i]
if
lowTemp>HourlyTemperatures[i]:
lowTemp=HourlyTemperatures[i]
if(i<=9):
print('0'+str(i)+':00%16s%s'%(' ',HourlyTemperatures[i]))
else:
print(str(i)+':00%16s%s'%(' ',HourlyTemperatures[i]))
print('High Temperature:
%d'%highTemp)
print('Low Temperature:
%d'%lowTemp)
print('Average Temperature:
%d'%AverageTemp)
#Create a main function
def main():
while(True):
HourlyTemperatures=[]
GetTemperatures(HourlyTemperatures)
avg=ComputeAverageTemp(HourlyTemperatures)
DisplayTemperatures(HourlyTemperatures,avg)
ch=input("Do you want to
test another set of data(y/n)? ")
if(ch=="n"):
break;
main()
------------------------------------------------------------------
Output
Enter temperature in 00:00 time in degrees: 35
Enter temperature in 01:00 time in degrees: 36
Enter temperature in 02:00 time in degrees: 36
Enter temperature in 03:00 time in degrees: 37
Enter temperature in 04:00 time in degrees: 37
Enter temperature in 05:00 time in degrees: 37
Enter temperature in 06:00 time in degrees: 38
Enter temperature in 07:00 time in degrees: 38
Enter temperature in 08:00 time in degrees: 39
Enter temperature in 09:00 time in degrees: 39
Enter temperature in 10:00 time in degrees: 40
Enter temperature in 11:00 time in degrees: 40
Enter temperature in 12:00 time in degrees: 41
Enter temperature in 13:00 time in degrees: 42
Enter temperature in 14:00 time in degrees: 43
Enter temperature in 15:00 time in degrees: 42
Enter temperature in 16:00 time in degrees: 41
Enter temperature in 17:00 time in degrees: 41
Enter temperature in 18:00 time in degrees: 40
Enter temperature in 19:00 time in degrees: 40
Enter temperature in 20:00 time in degrees: 39
Enter temperature in 21:00 time in degrees: 38
Enter temperature in 22:00 time in degrees: 35
Enter temperature in 23:00 time in degrees: 35
Hour
Temperature
00:00
35
01:00
36
02:00
36
03:00
37
04:00
37
05:00
37
06:00
38
07:00
38
08:00
39
09:00
39
10:00
40
11:00
40
12:00
41
13:00
42
14:00
43
15:00
42
16:00
41
17:00
41
18:00
40
19:00
40
20:00
39
21:00
38
22:00
35
23:00
35
High Temperature: 43
Low Temperature: 35
Average Temperature: 38
Do you want to test another set of data(y/n)? n