![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
Ubuntu 9.10 is out!!!
When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu. The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely. |
|
Programming Talk This forum is for all programming questions. The questions do not have to be directly related to Ubuntu and any programming language is allowed. |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Spilled the Beans
![]() |
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:
Quote:
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! ![]() EDIT: Yes, I have organized it well under the python file, but the forums seem to align allthe code to the left. Last edited by marcos.linux; August 4th, 2007 at 05:04 PM.. Reason: explanation |
|
|
|
|
|
|
#2 |
|
100% Pure Ubuntu
![]() Join Date: Aug 2006
Beans: 921
Kubuntu 8.04 Hardy Heron
|
Re: Accessing Global Variables under a Def in Python
First off, when posting source code, you should use the "code" tags (not "quote") so that code retains indentation.
As for your question, you just use the "global" keyword inside the function that needs access to a variable outside its scope. (You do NOT use the keyword when declaring the variable. So for instance: Code:
a=100
def question():
global a
print "question 1"
ques1=str(raw_input("What is 5+5"))
if ques1=="10":
print "Good!"
print "Current score: " + str(a)
else:
print "Wrong! Is 10."
a=a-1
print "Current score: " + str(a)
question()
EDIT: Note, however, that global variables are usually "bad style"... you should be using function variables wherever possible. For instance: Code:
starting_score=100
def question( score ):
print "question 1"
ques1=str(raw_input("What is 5+5"))
if ques1=="10":
print "Good!"
print "Current score: " + str(score)
else:
print "Wrong! Is 10."
score=score-1
print "Current score: " + str(score)
return score
current_score = starting_score
current_score = question(current_score)
Last edited by kebes; August 4th, 2007 at 05:13 PM.. |
|
|
|
|
|
#3 |
|
Day Old Decaf
![]() Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
|
Re: Accessing Global Variables under a Def in Python
Why str(score)? use just:
print "Current score: ", score or: print "Current score: %i points" % score Possibly good idea could be to define data structure with questions and correct answers (and scoring program using that structure), so it will be easier to add new tests. BTW "under a Def" means: "inside a function" |
|
|
|
|
|
#4 |
|
Spilled the Beans
![]() |
Thanks to all of u!
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|