【问题标题】:How do I add a while loop to an if statement for my multiple choice story?如何在我的多项选择故事的 if 语句中添加一个 while 循环?
【发布时间】:2018-12-27 00:08:30
【问题描述】:

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

我收到一条错误消息,说“行继续字符后出现意外字符。它在第一个引号之后突出显示我的最后一个 'if' 语句。如果我要取出它,它将突出显示最后一个 while True 语句。

您看到的# 来自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

【问题讨论】:

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


    【解决方案1】:

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

    • 第一个“break”缩进不正确。还需要一个标签。
    • 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
    

    【讨论】:

    • 对不起,实际上,中断是正确的。我在这篇文章中格式错误。这不是问题,我在运行时正确使用了它。我的第二个决定是给我错误信号的那些。
    • 我使用您的代码进行了编辑,现在我知道哪里出错了。谢谢!
    • 太棒了!我有蜱虫吗? :)
    【解决方案2】:

    第 17 行的换行符 (\n) 需要包含在字符串中(即,它需要在引号内) - 这就是导致特定错误消息的原因。

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

    while True:
        d1a = raw_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 = raw_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 = raw_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
    

    【讨论】:

    • 对不起,实际上,中断是正确的。我在这篇文章中格式错误。这不是问题,我在运行时正确使用了它。我的第二个决定是那些给我错误信号的决定。另外,我使用的是 python 3 而不是 2,所以 raw_input 不起作用。
    【解决方案3】:

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

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

    【讨论】:

    • @BadCoder 在 d2b 上也执行同样的操作 - 在您使用 ` to continue on next line - remove the ` 的所有行上执行此操作并关闭引号,而不是在下一行再次打开
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多