1
nameList = []
scoreList = []
gradeList = []
run = 1
loop = 1

def inputFunction():

className = input("\nWhat is the class name: ")
topic = input("What is the topic: ")
while run == 1:

    studentName = input("What is the students name? Enter 'done' to exit: ")

    if studentName in ("done", "Done"):
        break

    try:
        studentScore = int(input("What is the student's score? "))
    except ValueError:
        print("nonono")

    if studentScore <50:
        grade = "NA"
    if studentScore >49 and studentScore <70:
        grade = "A"
    if studentScore > 69 and studentScore < 90:
        grade = "M"
    if studentScore > 89:
        grade = "E"
    nameList.append(studentName)
    scoreList.append(studentScore)
    gradeList.append(grade)
print("\nClass Name:",className)
print("Class Topic:",topic)

I am trying to use a try and except for the variable "studentScore" however it gives me the error "name 'studentScore' is not defined" any help appreciated

2 Answers 2

1

You can use a while loop to keep asking the user for a valid integer until the user enters one:

while True:
    try:
        studentScore = int(input("What is the student's score? "))
        break
    except ValueError:
        print("Please enter a valid integer as the student's score.")
Sign up to request clarification or add additional context in comments.

Comments

0

The problm is, when the statement in the try block throws, studentScore is undefined.

You should give it a default value in case the exception is raised:

try:
    studentScore = int(input("What is the student's score? "))
except ValueError:
    print("nonono")
    studentScore = 0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.