简体   繁体   中英

how to make a raw_input call a function

I'm tinkering with a game by my own creation and I came across this problem. I am making the game in a very interactive fiction style but I can't make raw_input call a function like take sword to take(sword, room) to remove it from room and into inventory . First here's what I'm using for the game to call to continuously call the raw_input :

GameOn=True
while GameOn==True:
    a=raw_input("C:\>")
    a=a.lower()
    if a=="look":
        look()

Next is the functions:

def take(item,room):
    if item in tools:
        if item[0] in room:
            room.remove(item[0])
            inventory.append(item[1])
        print "taken"
    else:
        print item, "doesn't exist in this game"

def look(room):
    place=areas[room]
    for i in place:
        print i

And now for the lists:

sword=["There is a sword here", "sword"]
room=["There is a table in the corner", sword[0]]
inventory=[]
areas=[room,bedroom,living_room]
tools=[sword, shield, axe]

tools is there to show what is in the game and can be taken. areas and noun has more than one item in the list because a game with one room and one item is boring, so ignore everything in noun and areas that's not sword or room . The reason why the sword has two lists is so sword[0] appears in room while sword[1] appears in inventory . Ex.

print inventory
"sword"

look(room)
"There is a table in the corner"
"There is a sword here"

And how it will look in game:

C:\>look
"There is a table in the corner"
"There is a sword here"

I can get take(sword, room) to work(shown below) so I know it's not the code:

>>>room=["There is a table in the corner, sword[0]]
>>>inventory=[]
>>>look(room)
>"There is a table in the corner"
>"There is a sword here"
>>>print inventory
>

>>>take(sword, room)
>>>look(room)
>"There is a table in the corner"
>>>print inventory
>"sword"

I added '>>>' to show what is a variable or call of a function and '>' to show the response.

Now that that's covered, do I rewrite some of my code, am I doing something wrong in the raw_input , am I misunderstanding the capabilities of raw_input , or if not how do I get take sword to work?

It looks like you're doing pretty well. I'm not sure if I'm understanding completely, but I think you want to be keeping track of the current room that the user is in (I'm not sure if you're doing that already, it hasn't been specified). Once you do that, you already have the user's input stored in a .

You then want to split the input (assuming that the user types in the correct response "take sword") by spaces and check if the first word is "take". Then at that point, if the first word is "take", then you can just call your method.

a = raw_input("C:\>")
a = a.lower()
if a.split(' ')[0] == 'take': # split the array by spaces 
# (if the input is 'take sword', then [0] will be 'take' and [1] will be 'sword'
    take(a.split(' ')[1], current_room)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM