我试图在if语句中添加一个while循环,该循环附加到另一个while循环。我不知道我哪里出错了。我正在努力自学Python,所以我知道的不多。你知道吗
我收到一条错误消息,说“意外的字符后,行继续字符。它突出了我在第一个引号后面的最后一个if语句。如果我把它拿出来,它会突出最后一个真正的陈述。你知道吗
你看到的是来自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
在上面一行末尾的
\
后面可能有一个不可见的空格。我建议您不要使用连续字符,只需关闭字符串,然后在下一行中再次打开它,它就不那么脆弱,效果更好:第17行的新行字符(\n)需要包含在字符串中(即,它需要在引号中)-这就是导致特定错误消息的原因。你知道吗
此外,第6行的
break
语句需要缩进。除此之外,它应该工作-注意,您需要在终端中键入'a'
作为输入以使其工作-您可以使用raw_input
,然后只需键入a
在python中,获得正确的缩进和变量的范围是非常重要的。你知道吗
我把代码整理了一下。 注意:我在要打印的每一行文本周围加了双引号。更容易看。你知道吗
相关问题 更多 >
编程相关推荐