Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

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
Old August 4th, 2007   #1
marcos.linux
Spilled the Beans
 
marcos.linux's Avatar
 
Join Date: May 2007
Location: Puerto Rico
Beans: 12
Gutsy Gibbon Testing
Send a message via MSN to marcos.linux Send a message via Yahoo to marcos.linux
Question Accessing Global Variables under a Def in Python

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:
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!

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
marcos.linux is offline   Reply With Quote
Old August 4th, 2007   #2
kebes
100% Pure Ubuntu
 
kebes's Avatar
 
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..
kebes is offline   Reply With Quote
Old August 4th, 2007   #3
pmasiar
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"
pmasiar is offline   Reply With Quote
Old August 5th, 2007   #4
marcos.linux
Spilled the Beans
 
marcos.linux's Avatar
 
Join Date: May 2007
Location: Puerto Rico
Beans: 12
Gutsy Gibbon Testing
Send a message via MSN to marcos.linux Send a message via Yahoo to marcos.linux
Smile Re: Accessing Global Variables under a Def in Python

Thanks to all of u! this is has helped me completely.
marcos.linux is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:20 AM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry