s.chitti s.chitti - 6 months ago 10
Python Question

Can someone help, I don't understand why I'm getting an error that says my name isn't defined

I have defined

enterAllMarksofAStudenttoStore
below, but when I choose option 1 (if choice == 1) under the
while True
function I keep getting an error that say's
enterAllMarksofAStudenttoStore isn't defined
, but I defined it below.

I'm quite new to python can someone please help me.

def menu():

allStudentMarks = {}
while True:

print("welcome, your options are:")
print("1) Add student marks")
print("2) View student marks")
print("3) View graph")
print("4) Exit")
choice = input("choose your option:")
choice = int(choice)
if choice == 1:
allStudentMarks = enterAllMarksofAStudenttoStore(allStudentMarks)
enterAllStudentMarks()
elif choice == 2:
displayAllStudentReports()
elif choice == 3:
displayStudentOverallMarksBarChart()
print("Please enter student's information to view graph")
elif choice == 4:
break
else:
print("Please choose a value from 1 to 4")

def enterAllMarksofAStudenttoStore (allStudentMarks):

Student_name = input("Enter student's name:")
while True:
DF_Marks = input("Enter student's DF_Marks/n")
DF_Marks = float(DF_Marks)
if DF_Marks >=0 and DF_Marks <=20:
break
while True:
Project_Marks = input("Enter student's Project_Marks/n")
Project_Marks = float(projectMarks)
if Project_Marks >=0 and Project_Marks <=30:
break
while True:
Final_Exam_Marks = input("Enter student's Final_Exam_Marks/n")
Final_Exam_Marks = float(finalExamMarks)
if Final_Exam_Marks >=0 and Final_Exam_Marks <=50:
break
aStudentMarks = [DF_Marks, Project_Marks, Final_Exam_Marks]
allStudentMarks[Student_name] = aStudentMarks
return allStudentMarks

Answer

Fixes:

  1. Python is case-sensitive
  2. Its \n for a new line, not /n
  3. Try to keep consistency while naming variable.

Working code :

def enterAllMarksofAStudenttoStore(allStudentMarks):

    Student_name = input("Enter student's name:")
    while True:
            DFMarks = input("Enter student's DF_Marks\n")
            DF_Marks = float(DFMarks)
            if DF_Marks >=0 and DF_Marks <=20:
                    break
    while True:
            projectMarks = input("Enter student's Project_Marks\n")
            Project_Marks = float(projectMarks)
            if Project_Marks >=0 and Project_Marks <=30:
                    break
    while True:
            finalExamMarks = input("Enter student's Final_Exam_Marks\n")
            Final_Exam_Marks = float(finalExamMarks)
            if Final_Exam_Marks >=0 and Final_Exam_Marks <=50:
                    break
    aStudentMarks = [DF_Marks, Project_Marks, Final_Exam_Marks]
    allStudentMarks[Student_name] = aStudentMarks
    return allStudentMarks


def menu(allStudentMarks):
        while True:
                print("Welcome, your options are:")
                print("1) Add student marks")
                print("2) View student marks")
                print("3) View graph")
                print("4) Exit")
                choice = input("choose your option:")
                choice = int(choice)
                if choice == 1:
                                allStudentMarks = enterAllMarksofAStudenttoStore(allStudentMarks)
                                #enterAllStudentMarks()
                elif choice == 2:
                                displayAllStudentReports()
                elif choice == 3:
                                displayStudentOverallMarksBarChart()
                                print("Please enter student's information to view graph")
                elif choice == 4:
                                break
                else:
                                print("Please choose a value from 1 to 4")

if __name__ == "__main__":
        # execute only if run as a script
        allStudentMarks ={}
        menu(allStudentMarks)

Edit :

If you don't want to use this(which is equivalent to main in other languages like C, C++, ..) :

 if __name__ == "__main__":
            # execute only if run as a script
        allStudentMarks ={}
        menu(allStudentMarks)

You many just use: (put it in the end to make a call to the menu())

        allStudentMarks ={}
        menu(allStudentMarks)

I have use if __name__ == "__main__": so that the script runs only if required, otherwise if you import this script into another python script, it would automatically run it(as you will be making a call to in in the last line).

Comments