如何为我的多项选择故事的if语句添加while循环?

时间:2018-07-19 00:35:04

标签: python python-3.x if-statement while-loop

我试图在附加到另一个while循环的if语句中添加一个while循环。我不确定我要去哪里。我正在尝试自己学习Python,所以我不太了解。

我收到一条错误消息,指出“行继续符后出现意外字符。它突出显示了我在第一个引号之后的最后一个'if'语句。如果要删除它,它将突出显示了True语句的最后一个。< / p>

您看到的#来自another post

基本上,我的问题是如何为故事修复下一个while循环语句?对于以后的多项选择,过程是否相同?

while True:
    d1a = input ("Which do you inspect:\na) The back door?\nb) The basement?\n")
    # check if d1 is equal to one of the strings, specified in the list
    if d1a in ['a', 'b']:
        # if it was equal - break from the while loop
        break

# process the input
if d1a == "a": 
    print ("You approach the door.\n\
'Who's out there?'\n\
No one answers.\n\
You start to creep back into the kitchen but then there's tapping on the window.\n\
'Who's there? I'm warning you!'")
    while True:
        d2a = input ("What do you do:\na) Run outside to see who's there?\n\
b) Run back to your bedroom and hide underneath your bed?"\n)
        if d2a in ['a', 'b']:
            break

if d2a == "a":
    print ("You run out the door with a knife from the kitchen.\n\
You swing your head back and forth but see no one outside.")

elif d2a == "b":
    print ("You run up the stairs.\n\
There is a feeling of someone's hand on your back.\n\
It makes you run faster, not looking back.")


elif d1a == "b": 
    print ("You approach the basement.\n\
You go to turn on the light but it's flicking.\n\
You walk down the stairs. It's dim.\n\
You trip!\n\
'Ugh...'\n\
There's rustling under on the couch but you can't see what's on it.")
    while True:
        d2b = input ("What do you do:\na) Flash your flashlight on the couch?\n\
b) Ignore it and head back upstairs?")
        if d2b in ['a', 'b']:
            break

3 个答案:

答案 0 :(得分:1)

在python中,正确设置缩进和变量范围非常重要。

  • 第一个“中断”的缩进不正确。还需要一个标签。
  • d2a选项b中的\ n在双引号之外。
  • d2a响应的if语句缩进不正确。将它们再移一个标签。

我在这里整理了一些代码。 注意:我在要打印的每一行文本周围都加了双引号。看起来容易一点。

while True:
    d1a = input ("Which do you inspect:\n"\
                 "a) The back door?\n"\
                 "b) The basement?\n")

    # check if d1 is equal to one of the strings, specified in the list
    if d1a in ['a', 'b']:
        # if it was equal - break from the while loop break
        break

# process the input
if d1a == "a": 
    print ( "You approach the door.\n" \
            "'Who's out there?'\n" \
            "No one answers.\n" \
            "You start to creep back into the kitchen but then there's tapping on the window.\n" \
            "'Who's there? I'm warning you!'")
    while True:
        d2a = input ("What do you do:\n" \
                     "a) Run outside to see who's there?\n" \
                     "b) Run back to your bedroom and hide underneath your bed?\n")
        if d2a in ['a', 'b']:
            break

    if d2a == "a":
        print ("You run out the door with a knife from the kitchen.\n" \
               "You swing your head back and forth but see no one outside.")

    elif d2a == "b":
        print ("You run up the stairs.\n" \
               "There is a feeling of someone's hand on your back.\n" \
               "It makes you run faster, not looking back.")


elif d1a == "b": 
    print ("You approach the basement.\n" \
           "You go to turn on the light but it's flicking.\n" \
           "You walk down the stairs. It's dim.\n" \
           "You trip!\n" \
           "'Ugh...'\n" \
           "There's rustling under on the couch but you can't see what's on it.")

    while True:
        d2b = input ("What do you do:\n"\
                     "a) Flash your flashlight on the couch?\n" \
                     "b) Ignore it and head back upstairs?")
        if d2b in ['a', 'b']:
            break

答案 1 :(得分:0)

您可能在以上任一行的\之后有一个不可见的空格。我建议不要使用连续字符,而只需关闭字符串并在下一行中再次打开它,它就不那么脆弱了,并且效果更好:

d2b = input ("What do you do:\na) Flash your flashlight on the couch?\n”
    ”b) Ignore it and head back upstairs?")

答案 2 :(得分:0)

第17行的新行字符(\ n)必须包含在字符串中(即,它必须放在引号中),这就是导致该特定错误消息的原因。

此外,第6行的break语句需要缩进。除此之外,它还可以工作-请注意,您需要在终端中输入'a'作为输入才能使其正常工作-您可以改用raw_input,然后只需键入{{1} }

a