Bryan Haoui Bryan Haoui - 3 months ago 16
Python Question

Novice python user need help passing data between functions

I am finishing an assignment for a class where a teacher can input the student ID numbers and the students grades. The final grade will be calculated and returned next to the students number. I can calculate the final grade just fine, but I cannot append the grade to the list of student numbers.

def assignments():
assign1 = int(input("Assignment 1 grade: "))
if assign1 > 100:
print ("Please input a valid grade value.")
assign1 = int(input("Assignment 1 grade: "))
assign2 = int(input("Assignment 2 grade: "))
if assign2 > 100:
print ("Please input a valid grade value.")
assign2 = int(input("Assignment 2 grade: "))
assign3 = int(input("Assignment 3 grade: "))
if assign3 > 100:
print ("Please input a valid grade value.")
assign3 = int(input("Assignment 3 grade: "))
assign4 = int(input("Assignment 4 grade: "))
if assign4 > 100:
print ("Please input a valid grade value.")
assign4 = int(input("Assignment 4 grade: "))
assign5 = int(input("Assignment 5 grade: "))
if assign5 > 100:
print ("Please input a valid grade value.")
assign5 = int(input("Assignment 5 grade: "))
assign6 = int(input("Assignment 6 grade: "))
if assign6 > 100:
print ("Please input a valid grade value.")
assign6 = int(input("Assignment 6 grade: "))
assign7 = int(input("Assignment 7 grade: "))
if assign7 > 100:
print ("Please input a valid grade value.")
assign7 = int(input("Assignment 7 grade: "))
assign8 = int(input("Assignment 8 grade: "))
if assign8 > 100:
print ("Please input a valid grade value.")
assign8 = int(input("Assignment 8 grade: "))
assign9 = int(input("Assignment 9 grade: "))
if assign9 > 100:
print ("Please input a valid grade value.")
assign9 = int(input("Assignment 9 grade: "))
assign10 = int(input("Assignment 10 grade: "))
if assign10 > 100:
print ("Please input a valid grade value.")
assign10 = int(input("Assignment 10 grade: "))
assignGrade = assign1 + assign2 + assign3 + assign4 + assign5 + assign6 + assign7 + assign8 + assign9 + assign10
aGrade = assignGrade / 10
print("The final grade for all assignments is: ")
print(aGrade)
midterm = int(input("Midterm grade: "))
if midterm > 100:
print ("Please input a valid grade value.")
midterm = int(input("Midterm grade: "))
finalExam = int(input("Final Exam grade: "))
if finalExam > 100:
print ("Please input a valid grade value.")
finalExam = int(input("Final Exam grade: "))
testsGrade = midterm + finalExam
tGrade = testsGrade / 2
print("The final grade for all test is: ")
print(tGrade)
participation = int(input("Participation grade: "))
if participation > 100:
print ("Please input a valid grade value.")
participation = int(input("Participation grade: "))
partGrade = participation
print("The final grade for Participation is: ")
print(partGrade)
finalGrade = aGrade + tGrade + partGrade / 3


def students():
netIDList = []
maxLengthList = 6
while len(netIDList) < maxLengthList:
ID = input("Enter Student's Net ID: ")
netIDList.append(ID)
for s in netIDList:
print("Please input grades for student " + s)
assignments()
f = assignments().finalGrade
netIDList.append(": " + f)
print(netIDList)

def main():
students()

main()

Answer

Your problem is here:

f = assignments().finalGrade
netIDList.append(": " + f)

You're accessing assignments like it is an object rather than a function. You want to have assignments return a value to append.

def assignments():
   # do all your input and calculation here
   finalGrade = aGrade + tGrade + partGrade / 3
   return finalGrade

This way, the function will send this value back to the place where it was called from. Then, change

f = assignments().finalGrade

to

f = assignments()

EDIT: Your for loop should look basically like this:

for s in netIDList:
        print("Please input grades for student " + s)
        f = assignments()
        netIDList.append(": " + f)

but this will result in a somewhat confusing list:

[student1_name, student2_name, student1_score, student2_score]

So I would use a dictionary:

student_score_map = {}
for s in netIDList:
            print("Please input grades for student " + s)
            f = assignments()
            student_score_map[s] = f

This way you can print all student names with their score like so:

for s in netIDList:
    print(s + ' final score:' + student_score_map[s])

Take a look here for more on dictionaries