gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:09:45 25 4
gpt4 key购买 nike

我试图在附加到另一个 while 循环的 if 语句中添加一个 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 中,正确缩进和变量范围非常重要。

  • 第一个“break”缩进不正确。还需要一个标签。
  • d2a中的\n选项b在双引号外。
  • 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

关于python - 如何为我的多选故事的 if 语句添加 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51412726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com