Home > OS >  Passing data between a series of related functions
Passing data between a series of related functions

Time:07-24

I am attempting to create an exam grading program. I successfully wrote each function to do what I need it to, but when I attempt to execute the entire program, I am running into issues with my return variables not being referenced.

Here is my code for context:

def userinput():
    allinputs = []
    while True:
        try:
            results = list(map(int, input('Exam points and exercises completed: ').split(' ')))
            allinputs.append(results)
        except:
            ValueError
            break
    return allinputs

def points(allinputs):
    exampoints = []
    exercises = []
    exercises_converted = []
    gradepoints = []
    counter = 0
    for i in allinputs:
        exampoints.append(i[0])
        exercises.append(i[1])
    for e in exercises:
        exercises_converted.append(e//10)
    for p in exampoints:
        p2 = exercises_converted[counter]
        gradepoints.append(p   p2)
        counter  = 1
    return (gradepoints, exampoints, exercises_converted)

def average(gradepoints):
    avg_float = sum(gradepoints) / len(gradepoints)
    points_avg = round(avg_float, 1)
    return points_avg

def pass_percentage(exercises_converted, exampoints):
    failexam = []
    passexam = []
    passorfail = []
    i = 0
    while i < len(exampoints):
        if exampoints[i] < 10 or exercises_converted[i]   exampoints[i] < 15:
            failexam.append("fail")
            passorfail.append(0)
        else:
            passexam.append("pass")
            passorfail.append(1)
        i  = 1
    percentage_float = len(passexam)/len(failexam)
    percentage = round(percentage_float, 1)
    return (percentage, passorfail)

def grades_distribution(passorfail, gradepoints):
    grades = []
    i = 0
    l = 5
    while i < len(gradepoints):
        if passorfail[i] == 0:
            grades.append(0)
        elif passorfail[i] != 0 and gradepoints[i] >= 15 and gradepoints[i] <= 17:
            grades.append(1)
        elif passorfail[i] != 0 and gradepoints[i] >= 18 and gradepoints[i] <= 20:
            grades.append(2)
        elif passorfail[i] != 0 and gradepoints[i] >= 21 and gradepoints[i] <= 23:
            grades.append(3)
        elif passorfail[i] != 0 and gradepoints[i] >= 24 and gradepoints[i] <= 27:
            grades.append(4)
        elif passorfail[i] != 0 and gradepoints[i] >= 28 and gradepoints[i] <= 30:
            grades.append(5)
        i  = 1
    while l >= 0:
        print(l, ": ", "*" * grades.count(l))
    return


userinput()
print("Statistics:")
points(allinputs)
print(f"Points average: {average(gradepoints)}")
print(f"Pass percentage: {pass_percentage(exercises_converted, exampoints)}")
print("Grade distribution")
grades_distribution(passorfail, gradepoints)

I have no problems with the mechanics within each function; rather, my error lies where I try calling each of them from the main function.

The user input() function runs fine, and returns allinputs. However, when the second function points(allinputs) is called, the program states that the argument is undefined.

CodePudding user response:

You should store the return value of a function before passing it as argument. This should solve your problem

allinputs = userinput()
points(allinputs)
  • Related