Python 中的 while 循环使流程图变得复杂
Flowchart getting complicated with while loops in Python
我将不胜感激:
我试着做一个“那为什么要担心”哲学的形式程序:
我写了这段代码,但我不明白如何让 while 循环在每次用户没有在两个问题中输入“是”或“否”时重复自己。
problem = str(input("Do you have a problem in life? "))
problem = problem.replace(" ", "").lower() #nevermind caps or spaces
while problem:
if problem not in ("yes","no"):
print("Please enter YES or NO")
if problem == "no":
break
if problem == "yes":
something = str(input("Do you have something to do about it? "))
something = something.replace(" ","").lower()
while something:
if something not in ("yes","no"):
print("Please enter YES or NO")
elif:
break
print("Then why worry?")
我建议使用 while True
循环,这样你就可以把 input
代码放一次,然后在正确的条件下中断就可以了
while True:
problem = input("Do you have a problem in life? ").lower().strip()
if problem not in ("yes", "no"):
print("Please enter YES or NO")
continue
if problem == "no":
break
while True:
something = input("Do you have something to do about it? ").lower().strip()
if something not in ("yes", "no"):
print("Please enter YES or NO")
continue
break
break
print("Then why worry?")
使用海象运算符 (py>=3.8
) 可以更轻松地完成
while (problem := input("Do you have a problem in life? ").lower().strip()) not in ("yes", "no"):
pass
if problem == "yes":
while (something := input("Do you have something to do about it? ").lower().strip()) not in ("yes", "no"):
pass
print("Then why worry?")
你的算法是线性的,没有循环。因此,唯一需要循环的地方是当您尝试从用户那里获得正确的响应时。所以,我建议你把它移到一个函数中,然后你的例子变成这样:
def get_user_input(prompt):
while True:
reply = input(prompt).replace(" ", "").lower()
if reply in ['yes', 'no']:
return reply
print("Please enter YES or NO")
problem_exists = get_user_input("Do you have a problem in life? ")
if problem_exists == 'yes':
action_possible = get_user_input("Do you have something to do about it? ")
print("Then why worry?")
我将不胜感激:
我试着做一个“那为什么要担心”哲学的形式程序:
我写了这段代码,但我不明白如何让 while 循环在每次用户没有在两个问题中输入“是”或“否”时重复自己。
problem = str(input("Do you have a problem in life? "))
problem = problem.replace(" ", "").lower() #nevermind caps or spaces
while problem:
if problem not in ("yes","no"):
print("Please enter YES or NO")
if problem == "no":
break
if problem == "yes":
something = str(input("Do you have something to do about it? "))
something = something.replace(" ","").lower()
while something:
if something not in ("yes","no"):
print("Please enter YES or NO")
elif:
break
print("Then why worry?")
我建议使用 while True
循环,这样你就可以把 input
代码放一次,然后在正确的条件下中断就可以了
while True:
problem = input("Do you have a problem in life? ").lower().strip()
if problem not in ("yes", "no"):
print("Please enter YES or NO")
continue
if problem == "no":
break
while True:
something = input("Do you have something to do about it? ").lower().strip()
if something not in ("yes", "no"):
print("Please enter YES or NO")
continue
break
break
print("Then why worry?")
使用海象运算符 (py>=3.8
) 可以更轻松地完成
while (problem := input("Do you have a problem in life? ").lower().strip()) not in ("yes", "no"):
pass
if problem == "yes":
while (something := input("Do you have something to do about it? ").lower().strip()) not in ("yes", "no"):
pass
print("Then why worry?")
你的算法是线性的,没有循环。因此,唯一需要循环的地方是当您尝试从用户那里获得正确的响应时。所以,我建议你把它移到一个函数中,然后你的例子变成这样:
def get_user_input(prompt):
while True:
reply = input(prompt).replace(" ", "").lower()
if reply in ['yes', 'no']:
return reply
print("Please enter YES or NO")
problem_exists = get_user_input("Do you have a problem in life? ")
if problem_exists == 'yes':
action_possible = get_user_input("Do you have something to do about it? ")
print("Then why worry?")