1
from sys import exit

def dead(why):
    print why, "Better luck next time in the next life!"
    exit(0)

def strange_man():
    print """As you take way on to your adventure... You meet this strange man. 
    He tells you that he can not be trusted. He tells you that the door on the
    right is going to be good. He also tells you that the left door will lead you to good
    as well. But you cannot trust him... Which door do you pick?"""

    next = raw_input("Do you choose the left door or right? \n >")

    if next == "left" or "Left":
        good_door()
    elif next == "right" or "Right":
        evil_door()
    else:
        dead("You are drunk and can't seem to choose a door. Bye.")

def good_door():
    print """You seem to have chosen a door that has three distinct routes. You still do not know
    whether or not your choice of doors were correct... You hesitate to choose which route you take
    but you have to choose..."""

    next = raw_input("Which route do you choose? You have an option between Route 1, Route 2, or Route 3 \n >")

    if "Route 1" in next:
        print "Wow! You can't believe it! You are in heaven! You seem to have chosen the right door."
        exit(0)

    elif "Route 2" in next:
        desert()
    else:
        dead("You walk ahead into this route and then you suddenly fall and then a log hits your head, it seems like it was a trap. You're left a bloody mess.")

def desert():
    print """You walk into a desert and it's unbearably hot... You have two options... You stay here and hope
    that somebody will save you, or you keep walking... Which one do you choose?"""

    next = raw_input("Do you choose to stay or move? \n >")

    if "stay" in next:
        dead("You die of thirst")
    elif "move" in next:
        good_door()

def evil_door():
    print """You enter this door and you see two routes, which one do you choose?"""

    next = raw_input("You choose from the left route or the right route. \n >")
    if next == "left":
        good_door()
    else:
        dead("The route you chose seems to only go to Hell... You see satan and he eats you.")


strange_man()

我的问题出在奇怪的曼函数中,当我选择“正确”时,它会回到 good_door 函数而不是应该的 evil_door 函数......我不知道它为什么这样做!我似乎是对的:/

这是为我们制作自己的 CLI 文本游戏的 Learn Python The Hard Way 中的 ex36...

4

1 回答 1

4
next == "left" or "Left"

应该

next == "left" or next == "Left"

或者

next in ["left", "Left"]

"Left" 总是解析为 True 并且X or True永远为真。这同样适用于你所有的析取。

为了回答您的评论,我个人会这样实现:(这并不意味着您的解决方案不如我的可行,我只是尝试给出一些想法)

#! /usr/bin/python3
from sys import exit

class Location:
    def __init__ (self, welcome, menu, choices):
        self.welcome = welcome
        self.choices = choices
        self.menu = menu

    def enter (self, *args):
        if not self.choices:
            print (args [0] )
            exit (0)
        print ('\n\n{}\n{}'.format ('*' * 20, self.welcome) )
        while True:
            print ()
            print (self.menu)
            print ('\n'.join ('{}: {}'.format (index + 1, text) for index, text in enumerate (choice [0] for choice in self.choices) ) )
            try:
                choice = int (input () )
                return self.choices [choice - 1] [1:]
            except: pass

class World:
    locations = {
        'Beginning': Location ('As you take way on to your adventure... You meet this strange man.\nHe tells you that he can not be trusted.\nHe tells you that the door on the right is going to be good. He also tells you that the left door will lead you to good as well. But you cannot trust him...', 'Which door do you pick?', [ ('Left', 'GoodDoor', [] ), ('Right', 'BadDoor', [] ) ] ),
        'GoodDoor': Location ('You seem to have chosen a door that has three distinct routes. You still do not know whether or not your choice of doors were correct... You hesitate to choose which route you take but you have to choose...', 'Which route do you choose?', [ ('Route 1', 'Finish', ['You are in heaven!'] ), ('Route 2', 'Desert', [] ), ('Route 3', 'Finish', ['You trip and die.'] ) ] ),
        'BadDoor': Location ('You enter this door and you see two routes, which one do you choose?', 'You choose from the left route or the right route.', [ ('Left', 'GoodDoor', [] ), ('Right', 'Finish', ['You ended in hell.'] ) ] ),
        'Finish': Location ('', '', [] ),
        'Desert': Location ('You walk into a desert and it\'s unbearably hot... You have two options... You stay here and hope that somebody will save you, or you keep walking... Which one do you choose?', 'Do you choose to stay or move?', [ ('Stay', 'Finish', ['You die of thirst'] ), ('Move', 'GoodDoor', [] ) ] )
        }

    @classmethod
    def enter (bananaphone):
        room = World.locations ['Beginning']
        params = []
        while True:
            room, params = room.enter (*params)
            room = World.locations [room]

World.enter ()

嗯,语法高亮似乎弄乱了多行字符串文字。

我通过名称而不是简单地通过引用来引用位置的原因是为了允许图表中的循环。

于 2013-07-29T03:29:04.417 回答