HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

My (early stage) statistics homework assistant

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
homeworkassistantstatisticsstageearly

Problem

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:

Solution

Don't use recursion to redisplay a menu on invalid choices. With enough invalid input, the call stack will overflow. It's an ugly practice. Use a loop instead: repeat until user chooses to exit, print an error on invalid choices, call a function on valid choice. After exiting this loop, there will be no more statements to execute, so the program will exit naturally, without calling exit explicitly.

Use with for file operations:

with open(path) as fh:
    for line in fh:
        # ...


This way the file handle will be correctly closed when you exit the block.

Avoid sys.exit as much as possible. It's better to break out of the main program loop naturally, without explicitly exiting.

Avoid global variables. The load samples method could return the samples instead of seeing a global variable.

At the end of each operation you give the user the option to exit the program immediately. It would be better to control the program flow in the main loop, and have one common point of exit. It will reduce some repetition.

In the main menu, most choices lead to the execution of a method that contains the implementation of that action. Except when exiting, there is some code instead of a function call. It would be better to move that code to its own function.

Code Snippets

with open(path) as fh:
    for line in fh:
        # ...

Context

StackExchange Code Review Q#93939, answer score: 7

Revisions (0)

No revisions yet.