Error in code help please

Hello!

I am working on a project for a class, but I’ve found an error I cannot figure out how to solve. How do I get it to type the name that is entered earlier? I have bolded the line I am stuck on if that helps any.

Here is the code I have so far:

def main():
# declaring an empty list
list_score =
total_score = 0
# reading name
name = input("Input the Student name: ")
scores = input(“Enter five test scores separated by commas: “)
return [int(num) for num in scores.split(”,”)]
# loop for reading 5 test scores

def determine_grade(num):
if 90 <= num <= 100:
letter_grade = “A”
elif 80 <= num <= 89:
letter_grade = “B”
elif 70 <= num <= 79:
letter_grade = “C”
elif 60 <= num <= 69:
letter_grade = “D”
else:
letter_grade = “F”
return letter_grade

loop for computing average test score

def calc_average(grades):
average = sum(grades) / len(grades)
grade = determine_grade(average)
print(“Your final grade is: {:.1f} which is {}.”.format(average, grade))
print(‘Have a Great Summer!’, (name))

def show_letters(num, letter_grade):
print(“{:.1f} is {}\n”.format(num, letter_grade))

lst = main()
for n in lst:
show_letters(n, determine_grade(n))
calc_average(lst)

In the bolded line of code, just remove the parentheses around “name”. That would print out the value stored by the variable name:

print(‘Have a Great Summer!’, name)

But still you may find out that name is undefined when running your codes. This is because name is declared inside the function main(), which makes name a local variable only accessible inside main().

To fix it, you may add name in the return statement of main():

return name, [int(num) for num in scores.split(",")]

And change the line

lst = main()

into:

name, lst = main()

Alternatively, you may consider putting the codes in main() back to the main body of your program.

You may also notice the first two lines of main() may be redundant, as the variables list_score and total_score are unused afterward.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.