This program involves inputting multiple data items (temperatures) that are each
ID: 3728487 • Letter: T
Question
This program involves inputting multiple data items (temperatures) that are each within a certain range and then outputting a report about them utilizing lists and functions. Write a Python program that will do the following:
1. The program will input from 1 to 35 (inclusive) Fahrenheit temperatures from the user. You must first ask them to enter the number of temperatures that they will be typing in. If the value entered is not between 1 and 35, you need to display a message informing the user that the value they entered is out of range and make them re-input until they provide an acceptable value. Prompt every input.
2. The Fahrenheit values are floats and must range between -150.0 and 350.0 (inclusive.) If an out-of-range temperature is entered, you must display a message informing the user as such and have them re-input until an in-range value is entered.
3. Generate the output specified below.
Notes/Specifications:
1. You must create a list to hold your (float) temperature values.
2. All output needs to be right-justified and aligned by the decimal point.
3. Blank lines are only used to separate sections of the report (#5 below)
4. Display all floating-point (float) numbers to one decimal place.
5. The output must be done in this order:
a. The assignment and your name
b. The values in ascending order (in Fahrenheit and Celsius)
c. The average temperatures (for Fahrenheit and Celsius)
d. The highest and lowest temperatures (for Fahrenheit and Celsius)
e. The amount of temperatures above/equal to/and below the average (once)
f. The standard deviation for Fahrenheit. Use “corrected sample standard deviation” (n-1 divisor) http://en.wikipedia.org/wiki/Standard_deviation (be sure to address and avoid a potential divide by zero.)
6. Part of your grade is based on how well you break down the problem into smaller components. Each function should only handle one primary task (computing and outputting the same part of a report can be in the same function for this assignment – think about whether or not that’s best on a case-by-case basis.)
• NOTE: Taking all code and putting it into a function that’s called from __main__ or other attempts to circumvent this requirement will result in an extremely large deduction. Humongous, in fact.
7. The sort algorithm must be Selection Sort or better and not use any library functions.
8. Your code must be free of syntax and runtime errors.
9. Any variables being accessed in a function must be passed in as arguments and used as parameters. No direct global access (e.g. using global) is permitted. 11.The efficiency of each task is also being graded. Code optimally.
CIS 231 - Assiqnment 2-YourName Fahr Cels 30.0 40.0 50.0 4.4 10.0 40.0 50.0 30.0 Average: High: 10.0 Low: Above Average: Equal to Average: Below Average: Standard Deviation: 10.0Explanation / Answer
The following is the code to give the desired output.
Selection sort was used to sort the temperatures, basic error handling (in case of division by zero) done.
The program is divided in such a way that each task is handled by only one function which does nothing more than the name of the function.
Edit the line with the name part.
--------------------------------------------
import numbers
import math
def take_num_inp():
inp = input("Enter the number of temperatures: ")
return inp
def accept_temp(fahr_list):
inp = input("Enter Temperature between -150 and 350: ")
while not isinstance(inp, numbers.Real) or inp < -150 or inp>350:
inp = input("Wrong input. Enter Temperature between -150 and 350: ")
fahr_list.append(inp)
return fahr_list
def sel_sort(inp_list):
for i in range(len(inp_list)):
min = i
for j in range(i+1, len(inp_list)):
if inp_list[min] > inp_list[j]:
min = j
inp_list[i], inp_list[min] = inp_list[min], inp_list[i]
return inp_list
def convert_celsius(fahr_list, cel_list):
for item in fahr_list:
val = (item - 32) / 1.8
cel_list.append(val);
return cel_list
def find_avg(inp_list):
avg = sum(inp_list) / float(len(inp_list))
return avg
def find_above_avg(inp_list, avg):
num = sum(i > avg for i in inp_list)
return num
def find_below_avg(inp_list, avg):
num = sum(i < avg for i in inp_list)
return num
def find_std(fahr_list, avg):
sum = 0.0
for val in fahr_list:
sum = sum + math.pow(val - avg, 2)
if len(fahr_list) > 1:
sum = sum / (len(fahr_list)-1)
std = math.sqrt(sum)
else:
std = 0.0
return std
def output(fahr_list, cel_list, avg_fahr, avg_cel, above_avg, below_avg, equal_avg, std):
print(" ")
print("%45s "%("CSE 231 - Assignment 2 - Your Name"))
print("%20s %12s %12s"%("","Fahr","Cels"))
print("%20s %12s %12s"%("","----","----"))
for (item1, item2) in zip(fahr_list, cel_list):
print("%20s %12.1f %12.1f"%("",item1,item2))
print("%20s %12s %12s"%("","----","----"))
print("%-20s %12.1f %12.1f "%("Average:",avg_fahr,avg_cel))
print("%-20s %12.1f %12.1f "%("High:",fahr_list[-1],cel_list[-1]))
print("%-20s %12.1f %12.1f "%("Low:",fahr_list[0],cel_list[0]))
print("%-20s %12i"%("Above Average:",above_avg))
print("%-20s %12i"%("Equal to Average:",equal_avg))
print("%-20s %12i "%("Below Average:",below_avg))
print("%-20s %12.1f"%("Standard Deviation:",std))
return
def get_data(fahr_list, cel_list):
avg_fahr = find_avg(fahr_list)
avg_cel = find_avg(cel_list)
above_avg = find_above_avg(fahr_list, avg_fahr);
below_avg = find_below_avg(fahr_list, avg_fahr);
equal_avg = len(fahr_list) - above_avg - below_avg;
std = find_std(fahr_list,avg_fahr);
output(fahr_list, cel_list, avg_fahr, avg_cel, above_avg, below_avg, equal_avg, std);
return;
def main():
num = take_num_inp()
while not isinstance(num, numbers.Integral) or num<1 or num>35:
print("Please Enter an integer between 1 and 35")
num = take_num_inp()
fahr_list = []
cel_list = []
for i in range(num):
fahr_list = accept_temp(fahr_list)
fahr_list = sel_sort(fahr_list);
cel_list = convert_celsius(fahr_list, cel_list);
get_data(fahr_list, cel_list)
if __name__ == "__main__":
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.