Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 25th, 2005, 11:38 AM   #1
clanotheduck
Newbie
 
Join Date: Sep 2005
Posts: 6
Rep Power: 0 clanotheduck is on a distinguished road
Question Can a function give a global variable a value?

Is there anyway for this to happen?

At the moment, I am working on a little text adventure. I have also recently discovered the joy of functions, but they have one problem; they can't return a value for a global variable!

The following code is a test for a question system I am making for my text adventure game.

At the moment, I want my function to give a value for "answer".

inventory = [ "cat" , "gun" , "wallet" , "condom" ]

answer = 0


def questions(q,ansone,anstwo,ansoneres,anstwores,ansunknown):
    questionloop = 0
    while questionloop != 3:
        ans = raw_input(q)
        if ans == "inven":
            print inventory
        elif ans == ansone:
            print ansoneres
            questionloop = 3
            answer = 1
        elif ans == anstwo:
            print anstwores
            questionloop = 3
            answer = 2
        else:
            print ansunknown
       

questions("Where is my cat?! :","dead","alive","NOOOOOOOO!","THANK GOD!","WTF?!")

if answer == 1:
    print " answer equals 1, the story will continue with this path."
elif answer == 2:
    print " answer equals 2, the story will continue with this path."
else:
    print "no one is going to ever see this text. HAHAHAHAHA!"

And obviously, I always get the text no one will ever see.

Can anyone help me out here?
clanotheduck is offline   Reply With Quote
Old Sep 25th, 2005, 11:40 AM   #2
clanotheduck
Newbie
 
Join Date: Sep 2005
Posts: 6
Rep Power: 0 clanotheduck is on a distinguished road
I have no idea why this forum made a space in my local variable "ansuknown"
clanotheduck is offline   Reply With Quote
Old Sep 25th, 2005, 12:55 PM   #3
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
they can't return a value for a global variable!
Sure they can, but you're not returning a value. You're trying to set the value of a global variable in your function. Python can also do this, but you need to use the global keyword in your function to tell Python that you're referring a global "answer" variable, not a local one that you just created, so your function would be:
def questions(q,ansone,anstwo,ansoneres,anstwores,ansunknown):
    global answer
    questionloop = 0
    while questionloop != 3:
        ans = raw_input(q)
        if ans == "inven":
            print inventory
        elif ans == ansone:
            print ansoneres
            questionloop = 3
            answer = 1
        elif ans == anstwo:
            print anstwores
            questionloop = 3
            answer = 2
        else:
            print ansunknown
With that said, you should be returning a value from the function, not setting a global variable and checking it from somewhere else - that will get very messy and very difficult to maintain. You want to be returning a value from the function:
inventory = [ "cat" , "gun" , "wallet" , "condom" ]

def questions(q, ansone, anstwo, ansoneres, anstwores, ansunknown):
    questionloop = 0
    while questionloop != 3:
        ans = raw_input(q)
        if ans == "inven":
            print inventory
        elif ans == ansone:
            print ansoneres
            questionloop = 3
            return 1
        elif ans == anstwo:
            print anstwores
            questionloop = 3
            return 2
        else:
            print ansunknown

answer = questions("Where is my cat?! :", "dead", "alive", "NOOOOOOOO!", "THANK GOD!", "WTF?!")
...
Cerulean is offline   Reply With Quote
Old Sep 25th, 2005, 1:06 PM   #4
clanotheduck
Newbie
 
Join Date: Sep 2005
Posts: 6
Rep Power: 0 clanotheduck is on a distinguished road
oh wow, thanks a bunch!
clanotheduck is offline   Reply With Quote
Old Sep 25th, 2005, 1:56 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
If you're interested, here's a small piece of sample code for part of an adventure game. If you run this code you'll be given a prompt. "inventory" shows your inventory, "look hat/stick" looks at the specific items, and "quit" quits the program. More items and verbs can easily be added.

If you want me to, I'll give you a step-by-step run down of what it all means.

class Item:
   def __init__(self, name, description):
      self.name = name
      self.description = description

inventory = [ 
      Item("hat", "A battered green hat"),
      Item("stick", "A sturdy walking stick")
]

def a_an(word):
   if word[0] in "aeiou":
      return "an " + word
   else:
      return "a " + word

def view_inventory():
   for item in inventory:
      print a_an(item.name)

def look_at_item(name):
   matching_items = [item for item in inventory if item.name == name]
   first_item = matching_items[0]
   print first_item.description

verbs = {
   'inventory' : view_inventory,
   'look' : look_at_item
}

input = ""
while True:
   input = raw_input("> ")

   if input == "quit":
      break

   words = input.split()
   first_word = words[0]
   other_words = words[1:]

   try:
      verbs[first_word](*other_words)
   except TypeError:
      print "I don't know what you mean."
   except KeyError:
      print "I don't know what you mean."
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC