1

我在之前的冒险中使用以下代码做到了这一点:

def lobby():
invalid_check == True
while invalid_check == True: #This "while" loops exists solely to loop the command sequence should the user enter a bogus command.
    action = raw_input("> ")
    if "use" and "door" in action:
        print "You feel a sense of anxiousness as your hand lingers over the door knob, but your curiosity sets in -- you open the door and step through."
        invalid_check == False # It is now 'False' and will therefore allow the user to progress to the next 'room' or function.
        toy_room()
    elif "use" and "lever" in action:
        print "test"
    elif "examine" in action:
        print "Pretty simple here -- just a blue door and a red lever."
    elif "inventory" in action:
        inv()
    else:
        invalid()
        invalid_check == True

我做了一个 while 循环来提示用户,只要这是 True 并且每当我想停止输入提示操作时(例如,在死后或搬到新房间时),我都会将其设置为 False。

我的朋友为我编辑了我的代码,并说这是多余的——我想知道在用户输入操作后,有什么更有效的方法可以让我的输入在每个房间(每个房间都是自己的类)中出现?

例子:

输入 > 检查门

“呸呸呸门”

输入 > * <----- 再次出现提示 *

4

2 回答 2

1

我认为您将赋值 ( =) 与比较 ( ==) 混淆了。正确的代码如下所示:

def lobby():
    invalid_check = True
    while invalid_check:
        action = raw_input("> ")
        if "use" and "door" in action:
            print "You feel a sense of anxiousness as your hand lingers over the door knob, but your curiosity sets in -- you open the door and step through."
            invalid_check = False # It is now 'False' and will therefore allow the user to progress to the next 'room' or function.
            toy_room()
     ......
于 2013-06-07T14:06:41.567 回答
0

你是不是在想这样的事情:

while playing {
    if input is not None: 
        # Examine input
        if input == "exit":
            playing = False

    # Wait for user to input
    input = raw_input('input > ')
}
于 2013-06-07T14:00:14.813 回答