local variable 'marks' referenced before assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: local variable 'marks' referenced before assignment (/thread-27081.html) |
local variable 'marks' referenced before assignment - Calli - May-25-2020 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 RE: local variable 'marks' referenced before assignment - deanhystad - May-25-2020 while True: marks == input("Enter for subject " + str(nSubjects + 1) + ": ")Using comparison "==" where you want to use assignment "=" RE: local variable 'marks' referenced before assignment - Yoriz - May-25-2020 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) + ": ") RE: local variable 'marks' referenced before assignment - Calli - May-25-2020 Holly molly my eyes are getting weaker and weaker day by day :( thanks a lot fam. You're all the best |