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
  creating arbitrary local variable names Skaperen 9 2,946 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  how solve: local variable referenced before assignment ? trix 5 2,547 Jun-15-2024, 07:15 PM
Last Post: trix
  It's saying my global variable is a local variable Radical 5 10,712 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 4,258 Jan-10-2023, 10:58 PM
Last Post: snippsat
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 3,617 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 4,879 Mar-02-2021, 08:11 PM
Last Post: finndude
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 19,492 Dec-14-2020, 07:34 PM
Last Post: buran
  How to instantly add quotation marks and comma for parameters? cheers100 4 14,809 Oct-22-2020, 12:51 PM
Last Post: cheers100
  could someone explain keywords, marks, and function DrKatherineThuyMiller 14 8,245 Jul-23-2020, 07:14 PM
Last Post: DrKatherineThuyMiller
  Assignment of non-existing class variable is accepted - Why? DrZ 6 8,432 Jul-13-2020, 03:53 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.