![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 6
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Sep 2005
Posts: 6
Rep Power: 0
![]() |
I have no idea why this forum made a space in my local variable "ansuknown"
|
|
|
|
|
|
#3 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
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 ansunknowninventory = [ "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?!")
... |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2005
Posts: 6
Rep Power: 0
![]() |
oh wow, thanks a bunch!
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|