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()

私の問題は、strange_man 関数にあります。「正しい」を選択すると、本来の悪のドア関数ではなく、良いドアの関数に戻ります...なぜこれを行っているのかわかりません! 私はそれを正しく持っているようです:/

これは、独自の 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」X or Trueは常に True に解決され、常に 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 に答える