2 Replies - 1415 Views - Last Post: 03 July 2012 - 06:29 AM Rate Topic: -----

#1 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Error; Variable Referenced Before Assignment

Posted 03 July 2012 - 05:49 AM

Well here is my code, not much else to say really. Trying to add a score feature to the little script from my previous topic and I run into an error straight away. I really need to revise some of my skills. Anyway.


class Game(object):

	score = 0
	
	def easyq(self): #Loop
 
		while mode.lower() == "easy":
			#Addition
			f1 = randint(1,15)
			f2 = randint(1,15) 
			print "What is %r + %r?" % (f1, f2)
			answerIn = '%s' % (f1 + f2) 
			answer = raw_input("> ") 
			
			foundit = False
			
			for letter in lettersL: 
				if letter in answer: foundit = True
			for letter in lettersU:
				if letter in answer: foundit = True
			for symbol in symbols: 
				if symbol in answer: foundit = True
				
			if answer == "exit":
				print "Goodbye!"
				time.sleep(2)
				exit(0)
			elif answer == "pass":
				print "Question Passed!"
			elif foundit == True:
				print "Numbers Only! Question Passed!"
			elif answer == answerIn:
				score += 1
				print "Correct! Your Score: %d" % score
			else:
				print "Wrong!"


Here's the error:
Traceback (most recent call last):
   File "ex45.py", line 278, in <module>
      new_game.easyq()
   File "ex45.py", line 64, in easyq
      score += 1
UnboundLocalError: local variable 'score' referenced before assignment


Any help is greatly appreciated and sorry for the constant need of help! (I'm really struggling)

Is This A Good Question/Topic? 0
  • +

Replies To: Error; Variable Referenced Before Assignment

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Error; Variable Referenced Before Assignment

Posted 03 July 2012 - 06:15 AM

As you know score += 1 increases the current value of score by 1. The error message is telling you that score doesn't have a current value. You're probably thinking that it should have one because you set it to 0 on line 3. But that's a different variable.

The variable score defined on line 3 is defined outside of the method definition, so it's a member variable, not a local variable. If you want to access it from inside the easyq method, you need to refer to it as self.score, not just score.
Was This Post Helpful? 1
  • +
  • -

#3 Blargz1r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-June 12

Re: Error; Variable Referenced Before Assignment

Posted 03 July 2012 - 06:29 AM

When I get to that, I get "global variable 'score' is not defined".
I'm starting to think I should be going back and re-learning functions and classes.

Oh god, I was just looking at something completely different. Sorry for the double post but that error was from a line where I hadn't put in self.

Thank you a bunch for your help! Bloody life-saver!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1