Write a program that (1) accepts up to 15 positive real numbers, terminated with
ID: 3777575 • Letter: W
Question
Write a program that (1) accepts up to 15 positive real numbers, terminated with a final negative number that only acts as a sentinel (sentinel = indicates end of input data.) (2) Display the 9-option menu shown below and allow the user to select one of the 9 options by entering the appropriate menu value. (3) For every option, 1-8, calculate and return the value wanted. Then repeat steps (2) and (3) until the user selects option 9 to terminate the program. Hint-1: Sample-standard-deviation divides by n-1 Hint-2: Use the Windows calculator, “calc.exe”, or your scientific calculator and the 3 values shown below to check the validity of your sample-standard-deviation calculations. For example: (Blue text represents program output, Red text is user input)
Explanation / Answer
import math
ctr = 0
vals = []
n = input("Enter a set of up to 15 positive real numbers")
ctr += 1
vals.append(n)
while n>0 and ctr<15:
n = input("")
if n>0 :
vals.append(n)
choice = input("Select an option: 1. Display the values in ascending order 2. Display the smallest value entered 3. Display the largest value entered 4. Display the range of the values entered 5. Display the sum of the values entered 6. Display the mean (average value) 7. Display the median value 8. Display the sample standard deviation 9. Exit ")
while (choice < 9 and choice > 0):
vals_sorted = sorted(vals)
if choice == 1:
print "The values entered are: "
for val in vals_sorted:
print val+", "
elif choice == 2:
print "Smallest value:", vals_sorted[0]
elif choice == 3:
print "Largest value:", vals_sorted[-1]
elif choice == 4:
print "Range of values:", vals_sorted[-1]-vals_sorted[0]
elif choice == 5:
print "The sum =", sum(vals)
elif choice == 6:
print "The mean =", 1.0*sum(vals)/len(vals)
elif choice == 7:
if len(vals_sorted)%2==0:
print "The median =", (vals_sorted[len(vals_sorted/2)] + vals_sorted[len(vals_sorted/2)-1])/2.0
else:
print "The median =", vals_sorted[len((vals_sorted-1)/2)]
elif choice == 8:
variance = 0
mean = sum(vals)/len(vals)
for val in vals:
variance += (val-mean)**2
variance = 1.0*variance/len(vals)
std = math.sqrt(variance)
print "The sample standard deviation =", std
print "Goodbye!"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.