关于python:如何为我的多选故事的if语句添加while循环?

How do I add a while loop to an if statement for my multiple choice story?

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

我收到一条错误消息,说"线路延续字符后面的意外字符。它突出显示我在第一个引号后面的最后一个'if'语句。如果我要把它拿出来,它会突出显示最后一个True语句。

你看到的#是来自另一篇文章。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
while True:
    d1a = input ("Which do you inspect:
a) The back door?
b) The basement?
"
)
    # 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.
\
'Who's out there?'
\
No one answers.
\
You start to creep back into the kitchen but then there's tapping on the window.
\
'Who's there? I'm warning you!'"
)
    while True:
        d2a = input ("What do you do:
a) Run outside to see who's there?
\
b) Run back to your bedroom and hide underneath your bed?"

)
        if d2a in ['a', 'b']:
            break

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

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


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

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

  • 第一个"休息"错误缩进。还需要一个标签。
  • 双引号外的d2a选项中的 n。
  • d2a响应的if语句拼写错误。再将它们移出一个标签。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
while True:
    d1a = input ("Which do you inspect:
"
\
                "a) The back door?
"
\
                "b) The basement?
"
)

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

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

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


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

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


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
while True:
    d1a = raw_input ("Which do you inspect:
a) The back door?
b) The basement?
"
)
    # 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.
\
'Who's out there?'
\
No one answers.
\
You start to creep back into the kitchen but then there's tapping on the window.
\
'Who's there? I'm warning you!'"
)
    while True:
        d2a = raw_input ("What do you do:
a) Run outside to see who's there?
\
b) Run back to your bedroom and hide underneath your bed?
"
)
        if d2a in ['a', 'b']:
            break

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

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


elif d1a =="b":
    print ("You approach the basement.
\
You go to turn on the light but it's flicking.
\
You walk down the stairs. It's dim.
\
You trip!
\
'Ugh...'
\
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:
a) Flash your flashlight on the couch?
\
b) Ignore it and head back upstairs?"
)
        if d2b in ['a', 'b']:
            break


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

1
2
3
4
d2b = input ("What do you do:
a) Flash your flashlight on the couch?
"

   "b) Ignore it and head back upstairs?")