marcos.linux
August 4th, 2007, 10:03 PM
Hi, I am a noob at python, I was thinkng of making this python script for my college's exam wich asks you the question and you type the answer and a score will be shown, but I have a problem, I do not know how to make a variable inside a def access a global variable, for example:
a=100
def question():
print "question 1"
ques1=str(raw_input("What is 5+5"))
if ques1=="10":
print "Good!"
print "Current score: " + a
else:
print "Wrong! Is 10."
a=a-1
print "Current score: " + a
question()
As you can see something is wrong there! Well I get:
UnboundLocalError: local variable 'a' referenced before assignment
Thats where all my doubts starts running, how do I manage to make this work?
Any help would be hugely appreciated!:confused:
EDIT: Yes, I have organized it well under the python file, but the forums seem to align allthe code to the left.
a=100
def question():
print "question 1"
ques1=str(raw_input("What is 5+5"))
if ques1=="10":
print "Good!"
print "Current score: " + a
else:
print "Wrong! Is 10."
a=a-1
print "Current score: " + a
question()
As you can see something is wrong there! Well I get:
UnboundLocalError: local variable 'a' referenced before assignment
Thats where all my doubts starts running, how do I manage to make this work?
Any help would be hugely appreciated!:confused:
EDIT: Yes, I have organized it well under the python file, but the forums seem to align allthe code to the left.