使用Python返回if或else语句

时间:2017-09-27 14:32:21

标签: python python-3.x

经过一番环顾,这很可能是我正在寻找的,但我根本无法理解,也无法弄清楚如何将其应用到我的代码中。

Python Input Prompt Go Back

所以基本上我正在尝试制作一个基于文本的基本游戏,并且我有多个门会说某些事情/提示将要发生的事情,所以我想让用户有机会打开所有的门。

现在,如果他们选择了错误的门,他们就会被迫回去,这就是我想要的,但是如果他们选择了正确的门,他们就会被迫前进,我想补充一下返回“选项,但我无法弄清楚如何...  我对Python非常陌生,所以如果有人能以非常简单的方式帮助我,我会非常感激,谢谢大家,这是我的代码。

#Def#
def purple_door():'''I'd like it to have the option to type "continue" or "go back"'''
print('"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!')
input('continue')

def red_door():
    print('This door opens1')

def orange_door():
    print('This door opens2')

def yellow_door():
    print('This door opens3')

def green_door():
    print('This door opens4')

def blue_door():
    print('This door opens5')

def pink_door():
    print('This door opens6')

#End Def#

#If or Else#

while True:

    door_chosen = input('>')

    if door_chosen in ('Purple', 'purple', 'Purple.', 'purple.'):
        purple_door()
        break

    elif door_chosen in ('Red', 'red', 'Red.', 'red.'):
        red_door()

    elif door_chosen in ('Orange', 'orange', 'Orange.', 'orange.'):
        orange_door()

    elif door_chosen in('Yellow', 'yellow', 'Yellow.', 'yellow.'):
        yellow_door()

    elif door_chosen in('Green', 'green', 'Green.', 'green.'):
        green_door()

    elif door_chosen in ('Blue', 'blue', 'Blue.', 'blue.'):
        blue_door()

    elif door_chosen in ('Pink', 'pink', 'Pink.', 'pink.'):
        pink_door()

    else:
        print('Please type a color stated above.')
#If or else end#

非常感谢任何改进建议。

编辑:我想我的解释不够好。

是的,当选择紫色以外的任何门时,它会强迫你选择另一个,直到你选择紫色,这就是我想要发生的事情。

但是让我们说有人想打开所有的门,他们的第一个猜测是紫色,现在他们必须继续前进,所以我想做的就是选择回去,比如说。 ///例/// “选择你要打开哪扇门”

“你打开紫色的门进入大厅” “继续,或回去”

回去

“你决定回去看看其他门” ///完///

然后您可以选择紫色并在准备好后继续。 我希望现在更有意义。

2 个答案:

答案 0 :(得分:0)

您的程序不允许用户返回的原因是因为您可能只测试包含break语句的紫门。每隔一扇门就会要求用户选择另一扇门:

另请注意我使用.lower()进行的更改,以减少您需要检查的值

def purple_door():
    print('"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!')

def red_door():
    print('This door opens1')

while True:
    door_chosen = input('Choose a door colour: ')

    if door_chosen.lower() in ('purple', 'purple.'):
        purple_door()
    elif door_chosen.lower() in ('red', 'red.'):
        red_door()
    else:
        print('Please type a color stated above.')

示例输出:

Choose a door colour: red
This door opens1
Choose a door colour: purple
"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!"
Choose a door colour: black
Please type a color stated above.
Choose a door colour: red.
This opens door1

答案 1 :(得分:0)

您可以做的是将代码的#If Else#部分转换为函数。从那里,你可以在purple_door的末尾调用该函数,如果有一些输入==返回。我一路上做了一些其他的可读性改进:

color_num = {'red': 1, 'orange': 2, 'yellow': 3, 'green': 4, 'blue': 5, 'pink': 6}  # for later use


def purple_door():
    print('"You open the door to find a long dark hall, at the end you can see a glowing white light, perhaps a shiny medal! Oooo shiny medal!"')
    while True:
        choice = input('Continue? ').lower().strip(' .')

        if choice in {'continue', 'yes', 'ok', 'why not', 'may as well'}:
            # Continue the game
            break

        elif choice in {'no', 'back', 'go back', 'do not continue', 'retreat'}:
            main()
            break

        else:
            continue

def main():
    while True:
        door_chosen = input('>').lower().strip(' .')  # lower forces the string to lowercase
                                                      # and strip(' .') removes spaces and periods from the beginning and end
        if door_chosen == 'purple':
            purple_door()
            break

        elif door_chosen in color_num.keys():
            print('This door opens{}'.format(color_num[door_chosen]))  # inserts the number corresponding to the door in place of the {}

        else:
            print('Please type a color stated above.')

main()