Python 挣扎于While循环
我正在用python编写一个while循环,该循环继续要求用户输入,直到他们键入quit这个词。每当我运行代码并键入单词“quit”时,它就会继续运行 这是我到目前为止得到的代码:Python 挣扎于While循环,python,Python,我正在用python编写一个while循环,该循环继续要求用户输入,直到他们键入quit这个词。每当我运行代码并键入单词“quit”时,它就会继续运行 这是我到目前为止得到的代码: x = input("Enter a word or type quit to exit:") while x: input("Enter a word or type quit to exit:") if x == Quit.lower(): print(&q
x = input("Enter a word or type quit to exit:")
while x:
input("Enter a word or type quit to exit:")
if x == Quit.lower():
print("Thanks for playing!")
如有任何建议,将不胜感激 如果if语句在while循环之外,那么只会在while循环之后检查“if”函数。您还应该在“退出”周围添加“-”符号 所以 一些问题:
if
语句必须在while循环中,而不是在while循环之外
而x
将不起作用
x = input("Enter a word or type quit to exit:") #Takes input
while x != "quit": # Checks if it is "quit".
# Do something...
x = input("Enter a word or type quit to exit:")# Takes input again.
print("Thanks for playing!")
如果
块不在for
循环的范围内,则执行。此外,您从未定义变量Quit
。
x = input("Enter a word or type quit to exit:") #Takes input
while x != "quit": # Checks if it is "quit".
# Do something...
x = input("Enter a word or type quit to exit:")# Takes input again.
print("Thanks for playing!")