statistics in python program wont output what i expect
Question:
I am trying to do a program for a class and it is supposed to take multiple values and output user selected statistic data. however all it does is asks for another selection.
Here is the code I have:
import statistics
values=[12,22,56,44,33,25,18,5,33,66]
print('#####################################################')
print('Choose from the MENU to perform given task ')
print('1- Calculate the mean value for the List')
print('2- Calculate the median value for the List')
print('3- Calculate the mode value for the List')
print('4- Calculate the Range value for the List')
print('5- Sort the values in ascending order in the List')
print('6- Calculate the standard deviation for the List')
print('-1 To EXIT')
print('#####################################################')
for i in range(7):
user_in=int(input('What is your choice? '))
if user_in == -1:
break
else:
while user_in != -1:
if user_in == 1:
mean_val=statistics.mean(values)
print(f'Mean is {mean_val:.2f}')
break
elif user_in == 2:
median_val=statistics.median(values)
print(f'Median is {median_val:.2f}')
break
elif user_in == 3:
mode_val=statistics.mode(values)
print(f'Mode is {mode_val:.2f}')
break
elif user_in == 4:
range_val=max(values)-min(values)
print(f'Range is {range_val}')
break
elif user_in == 5:
sort_val=sorted(values)
print(f'Sorted {sort_val}')
break
elif user_in == 6:
sd_dev=statistics.stdev(values)
print(f'Standard Deviation {sd_dev:.2f}')
break
else:
print('Invalid Entry')
break
print('EXIT is selected')
I was expecting some sort of answer to be output but all that outputs is the program asking for selection.
Answers:
bro you have an indentation mistake. Your if/else need to have the same indentation. then it works. else be careful or u burn your hand! Look below the code:
import statistics
values=[12,22,56,44,33,25,18,5,33,66]
print('#####################################################')
print('Choose from the MENU to perform given task ')
print('1- Calculate the mean value for the List')
print('2- Calculate the median value for the List')
print('3- Calculate the mode value for the List')
print('4- Calculate the Range value for the List')
print('5- Sort the values in ascending order in the List')
print('6- Calculate the standard deviation for the List')
print('-1 To EXIT')
print('#####################################################')
for i in range(7):
user_in=int(input('What is your choice? '))
if user_in == -1:
break
else:
while user_in != -1:
if user_in == 1:
mean_val=statistics.mean(values)
print(f'Mean is {mean_val:.2f}')
break
elif user_in == 2:
median_val=statistics.median(values)
print(f'Median is {median_val:.2f}')
break
elif user_in == 3:
mode_val=statistics.mode(values)
print(f'Mode is {mode_val:.2f}')
break
elif user_in == 4:
range_val=max(values)-min(values)
print(f'Range is {range_val}')
break
elif user_in == 5:
sort_val=sorted(values)
print(f'Sorted {sort_val}')
break
elif user_in == 6:
sd_dev=statistics.stdev(values)
print(f'Standard Deviation {sd_dev:.2f}')
break
else:
print('Invalid Entry')
break
print('EXIT is selected')
I am trying to do a program for a class and it is supposed to take multiple values and output user selected statistic data. however all it does is asks for another selection.
Here is the code I have:
import statistics
values=[12,22,56,44,33,25,18,5,33,66]
print('#####################################################')
print('Choose from the MENU to perform given task ')
print('1- Calculate the mean value for the List')
print('2- Calculate the median value for the List')
print('3- Calculate the mode value for the List')
print('4- Calculate the Range value for the List')
print('5- Sort the values in ascending order in the List')
print('6- Calculate the standard deviation for the List')
print('-1 To EXIT')
print('#####################################################')
for i in range(7):
user_in=int(input('What is your choice? '))
if user_in == -1:
break
else:
while user_in != -1:
if user_in == 1:
mean_val=statistics.mean(values)
print(f'Mean is {mean_val:.2f}')
break
elif user_in == 2:
median_val=statistics.median(values)
print(f'Median is {median_val:.2f}')
break
elif user_in == 3:
mode_val=statistics.mode(values)
print(f'Mode is {mode_val:.2f}')
break
elif user_in == 4:
range_val=max(values)-min(values)
print(f'Range is {range_val}')
break
elif user_in == 5:
sort_val=sorted(values)
print(f'Sorted {sort_val}')
break
elif user_in == 6:
sd_dev=statistics.stdev(values)
print(f'Standard Deviation {sd_dev:.2f}')
break
else:
print('Invalid Entry')
break
print('EXIT is selected')
I was expecting some sort of answer to be output but all that outputs is the program asking for selection.
bro you have an indentation mistake. Your if/else need to have the same indentation. then it works. else be careful or u burn your hand! Look below the code:
import statistics
values=[12,22,56,44,33,25,18,5,33,66]
print('#####################################################')
print('Choose from the MENU to perform given task ')
print('1- Calculate the mean value for the List')
print('2- Calculate the median value for the List')
print('3- Calculate the mode value for the List')
print('4- Calculate the Range value for the List')
print('5- Sort the values in ascending order in the List')
print('6- Calculate the standard deviation for the List')
print('-1 To EXIT')
print('#####################################################')
for i in range(7):
user_in=int(input('What is your choice? '))
if user_in == -1:
break
else:
while user_in != -1:
if user_in == 1:
mean_val=statistics.mean(values)
print(f'Mean is {mean_val:.2f}')
break
elif user_in == 2:
median_val=statistics.median(values)
print(f'Median is {median_val:.2f}')
break
elif user_in == 3:
mode_val=statistics.mode(values)
print(f'Mode is {mode_val:.2f}')
break
elif user_in == 4:
range_val=max(values)-min(values)
print(f'Range is {range_val}')
break
elif user_in == 5:
sort_val=sorted(values)
print(f'Sorted {sort_val}')
break
elif user_in == 6:
sd_dev=statistics.stdev(values)
print(f'Standard Deviation {sd_dev:.2f}')
break
else:
print('Invalid Entry')
break
print('EXIT is selected')