Python Forum
local variable 'marks' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local variable 'marks' referenced before assignment
#1
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
Reply
#2
    while True:
        marks == input("Enter for subject " + str(nSubjects + 1) + ": ")
Using comparison "==" where you want to use assignment "="
Reply
#3
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) + ": ")
Reply
#4
Holly molly my eyes are getting weaker and weaker day by day :( thanks a lot fam. You're all the best
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,090 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 1,465 Jan-10-2023, 10:58 PM
Last Post: snippsat
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,855 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 3,223 Mar-02-2021, 08:11 PM
Last Post: finndude
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 11,258 Dec-14-2020, 07:34 PM
Last Post: buran
  How to instantly add quotation marks and comma for parameters? cheers100 4 7,913 Oct-22-2020, 12:51 PM
Last Post: cheers100
  could someone explain keywords, marks, and function DrKatherineThuyMiller 14 4,371 Jul-23-2020, 07:14 PM
Last Post: DrKatherineThuyMiller
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,186 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,227 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  Variable assignment wierdness with Memory eoins 1 2,003 Mar-08-2020, 10:15 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020