如何在Python中为基于文本的游戏创建退出按钮?

如何在Python中为基于文本的游戏创建退出按钮?,python,Python,我想让我的代码在循环中运行,当用户输入“end”时,程序关闭。我是这样做的 T = True while T: startInput = input() # insert code while True: if startInput == "end" or startInput == "End": T = False break 问题是它不起作用。试试这个: T = True while T: startI

我想让我的代码在循环中运行,当用户输入“end”时,程序关闭。我是这样做的

T = True
while T:
    startInput = input()
    # insert code
while True:
    if startInput == "end" or startInput == "End":
    T = False
    break
问题是它不起作用。

试试这个:

T = True
while T:
    startInput = input()
    # insert code
    if startInput == "end" or startInput == "End":
        T = False  
        break

问题是,您运行的是第一个循环,而它从未运行过第二个循环,因此它永远无法提取该代码并跳出循环。

如果您使用的是上面的python 3.8,您可以这样使用:

T = True
while (startInput := input()) not in ["end","END"]:   
    # insert code
T=False 

尝试删除第二行
,同时为True
并缩进
T=False
break
这两行。