Python Forum

Full Version: local variable 'marks' referenced before assignment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The error i am getting is

UnboundLocalError: local variable 'marks' referenced before assignment

def main(): totalMarks = 0 nSubjects = 0 while True: marks == input("Enter for subject " + str(nSubjects + 1) + ": ") if marks == '': break marks = float(marks) if marks < 0 or marks > 100: print("INVALID MARKS !!") continue nSubjects = nSubjects + 1 totalMarks += marks percentage = totalMarks / nSubjects print("Total marks: ",int(totalMarks)) print("Number of Subjects: ",nSubjects) print("Percentage: ", round(percentage, 2)) if __name__ == "__main__": main()
But if i set the marks to 0 the if statement is not executed

Quote: totalMarks = 0
nSubjects = 0
marks = 0

Wall
 while True: marks == input("Enter for subject " + str(nSubjects + 1) + ": ")
Using comparison "==" where you want to use assignment "="
You have double == which does a comparison
marks == input("Enter for subject " + str(nSubjects + 1) + ": ")
you want a single = to assign
marks = input("Enter for subject " + str(nSubjects + 1) + ": ")
Holly molly my eyes are getting weaker and weaker day by day :( thanks a lot fam. You're all the best