我正在创建一个通过用户输入调用函数的文字冒险游戏。在 start() 中输入 1 会调用 start_observe,但 2 会调用 start()。为什么?
I'm creating a text adventure game that calls functions through user input. Inputting 1 at start() does call start_observe, but 2 calls start(). Why?
这是带有可用选项的代码。我不知道发生了什么!
def start():
slow_print("""_
It's Monday, 9:33pm, August 4th, 2024.
You lay in the back of an old truck headed ...yada yada... emember the last time you were awake.
Enter (1) to observe your surroundings.
Enter (2) to scream your lungs out.
Enter (3) to climb out of the truck.
Enter (4) to hear what's going on around you.
_""", 1)
choice = input("--> ")
reset_console()
“1”调用了 start_observe(),但“2”没有调用 start_scream(),我很困惑!
if "1" in choice:
start_observe()
else:
start()
if "2" in choice:
start_scream()
else:
start()
if "3" in choice:
我唯一能想到的是我没有正确缩进,或者函数没有按照它们需要的顺序排列(虽然我测试了但不是这样)。
更新,我将其更改为这种嵌套的 if 语句样式并且它有效,如果我这样更新,我 运行 会不会有进一步的问题?
choice = input("--> ")
reset_console()
if "1" in choice:
start_observe()
else:
if "2" in choice:
start_scream()
else:
if "3" in choice:
start_climb()
else:
if "4" in choice:
start_hear()
else:
start()
另外,谢谢路德的帮助!
更新 #2:哈哈,好吧,我很笨。在我意识到 else 之后,我改为这样做了:if equals elif。哈哈哈。
if "1" in choice:
start_observe()
elif "2" in choice:
start_scream()
elif "3" in choice:
start_climb()
elif "4" in choice:
start_hear()
else:
start()
假设 choice
是 '2'
,并且此代码运行:
if "1" in choice: # False
start_observe()
else: # This clause runs.
start()
如果此代码在 start
函数中,它将在代码运行后开始递归调用。
这是带有可用选项的代码。我不知道发生了什么!
def start():
slow_print("""_
It's Monday, 9:33pm, August 4th, 2024.
You lay in the back of an old truck headed ...yada yada... emember the last time you were awake.
Enter (1) to observe your surroundings.
Enter (2) to scream your lungs out.
Enter (3) to climb out of the truck.
Enter (4) to hear what's going on around you.
_""", 1)
choice = input("--> ")
reset_console()
“1”调用了 start_observe(),但“2”没有调用 start_scream(),我很困惑!
if "1" in choice:
start_observe()
else:
start()
if "2" in choice:
start_scream()
else:
start()
if "3" in choice:
我唯一能想到的是我没有正确缩进,或者函数没有按照它们需要的顺序排列(虽然我测试了但不是这样)。
更新,我将其更改为这种嵌套的 if 语句样式并且它有效,如果我这样更新,我 运行 会不会有进一步的问题?
choice = input("--> ")
reset_console()
if "1" in choice:
start_observe()
else:
if "2" in choice:
start_scream()
else:
if "3" in choice:
start_climb()
else:
if "4" in choice:
start_hear()
else:
start()
另外,谢谢路德的帮助!
更新 #2:哈哈,好吧,我很笨。在我意识到 else 之后,我改为这样做了:if equals elif。哈哈哈。
if "1" in choice:
start_observe()
elif "2" in choice:
start_scream()
elif "3" in choice:
start_climb()
elif "4" in choice:
start_hear()
else:
start()
假设 choice
是 '2'
,并且此代码运行:
if "1" in choice: # False
start_observe()
else: # This clause runs.
start()
如果此代码在 start
函数中,它将在代码运行后开始递归调用。