I wanted to share my code for others to critique. This program is something I started while struggling with homework problems in my Statistics course, and after getting the ball rolling, I decided to keep adding and perfecting it. Right now it is in a usable state, but I want to hear the opinion of other more experienced coders. Coding is something I'm passionate about and I need to know where my knowledge and methodology stands.
import statistics
import sys
samples = []
def menu():
print("Welcome to the Statistics Assistant 3000")
print("Select from the following options:")
print(" 1. Find the mean")
print(" 2. Find the median")
print(" 3. Find the mode")
print(" 4. Find the range")
print(" 5. Find the standard deviation")
print(" 6. Find the z-score")
print(" 0. Quit program")
choice = input("Pick one: ")
if choice == '1':
find_mean()
elif choice == '2':
find_median()
elif choice == '3':
find_mode()
elif choice == '4':
find_range()
elif choice == '5':
standard_deviation()
elif choice == '6':
z_score()
elif choice == '0':
f = open('sultry.txt')
for line in f:
print(line)
sys.exit()
else:
print("That isn't one of the options")
print("Try again")
menu()
def load_samples():
global samples
print("Would you like to load samples from a text file")
print("or enter each sample manually?")
print(" 1. File")
print(" 2. Manually")
print(" 3. Return")
user_response = input("> ")
if "1" in user_response:
# force_open = os.startfile('sample_list.txt')
# input("Hit enter when done saving data...")
f = open("sample_list.txt")
samples = f.readlines()
samples = [float(x) for x in samples]
# samples = [int(x) for x in samples]
return samples
elif "2" in user_response:
while True:
print("Enter a value. Enter 'done' when finished")
value = input('>')
try:
if 'done' in value.lower():
print("You entered done")
print("Done? [Y/N]")
user_response = input(">")
if 'Y' or 'y' in user:
return samples
menu()
else:
value = int(value)
samples.append(value)
print(samples)
except:
print("You need to enter an integer")
elif "3" in user_response:
menu()
else:
print("I don't recognize that option")
def quit():
print("Than you for using this application")
sys.exit()
def find_mean():
global samples
load_samples()
mean = round(statistics.mean(samples), 1)
print("The mean = " + str(mean))
print("Return to menu?")
print("[Y/N]")
user_input = input(">")
user_input = user_input.lower()
if 'y' in user_input:
menu()
else:
quit()
def find_median():
global samples
load_samples()
result = statistics.median(samples)
print("The median is %s" % (result))
print("Return to menu?")
print("[Y/N]")
user_input = input(">")
user_input = user_input.lower()
if 'y' in user_input:
menu()
else:
quit()
def find_mode():
global samples
load_samples()
result = statistics.mode(samples)
print("The mode is %s" % (result))
print("Return to menu?")
print("[Y/N]")
user_input = input(">")
user_input = user_input.lower()
if 'y' in user_input:
menu()
else:
quit()
def find_range():
global samples
load_samples()
x = min(samples)
y = max(samples)
result = (y - x)
print("The range of %s and %s is %s" % (x, y, result))
print("Return to menu?")
print("[Y/N]")
user_input = input(">")
user_input = user_input.lower()
if 'y' in user_input:
menu()
else:
quit()
def standard_deviation():
global samples
load_samples()
stan_dev = statistics.stdev(samples)
print('{:.03}'.format(stan_dev))
print("Return to menu?")
print("[Y/N]")
user_input = input(">")
user_input = user_input.lower()
if 'y' in user_input:
menu()
else:
quit()
def z_score():
global samples
load_samples()
stan_dev = statistics.stdev(samples)
mean = statistics.mean(samples)
middle = statistics.median(samples)
z = round(((middle - mean) / stan_dev), 2)
print("The Z-Score is " + str(z))
print("Return to menu?")
print("[Y/N]")
user_input = input(">")
user_input = user_input.lower()
if 'y' in user_input:
menu()
else:
quit()
if __name__ == '__main__':
try:
menu()
except:
print("Press ENTER to exit...")
input()