CLI似乎忽略了我的功能,而转到另一个!(Python)

2024-04-25 04:40:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的问题是奇怪的门函数,当我选择“正确”时,它返回到好的门函数,而不是邪恶的门函数。。。我不知道它为什么这样做!我好像说得对:/

这是为ex36在学习Python的艰难的方式,我们使我们自己的CLI文本游戏。。。你知道吗


Tags: orandthetoinyoudefleft
1条回答
网友
1楼 · 发布于 2024-04-25 04:40:43
next == "left" or "Left"

应该是

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

或者

next in ["left", "Left"]

“Left”总是解析为True,X or 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 ()

嗯,语法荧光笔似乎弄乱了多行字符串文字。你知道吗

我之所以按名称而不是简单地按引用引用引用位置,是为了在图中允许循环。你知道吗

相关问题 更多 >