Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

statistics in python program wont output what i expect

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading