Python嵌套';而';循环未正确执行
我是Python新手。我想出了一个简单的程序来测试我学到的一些工具。除了我的一个嵌套“while”循环外,它在大多数情况下都有效。下面是我的代码。不起作用的部分是当我在我的工作功能中输入“手动”时,它正在“下雨”。我打算让它打印rainedOut,然后返回原始输入。只有当雨下了3次(即,在循环回到原始输入3次后,雨就出来了)时,它才应该打印“您可能现在就应该放弃。”并退出该功能。然而,它所做的是,在第一次运行时,它会连续打印rainedOut 3次,然后自动结束函数。有人能帮我解决代码中的错误吗Python嵌套';而';循环未正确执行,python,while-loop,Python,While Loop,我是Python新手。我想出了一个简单的程序来测试我学到的一些工具。除了我的一个嵌套“while”循环外,它在大多数情况下都有效。下面是我的代码。不起作用的部分是当我在我的工作功能中输入“手动”时,它正在“下雨”。我打算让它打印rainedOut,然后返回原始输入。只有当雨下了3次(即,在循环回到原始输入3次后,雨就出来了)时,它才应该打印“您可能现在就应该放弃。”并退出该功能。然而,它所做的是,在第一次运行时,它会连续打印rainedOut 3次,然后自动结束函数。有人能帮我解决代码中的错误吗
import time
import sys
done = "I'm tired of you. Goodbye."
rainedOut = "Sorry, rain foiled your plans :("
dontUnderstand = "I'm sorry, I don't understand."
def good_weather():
"""Imagine a world where every 5 seconds it rains (good_weather = False),
then is sunny again (good_weather = True). This function should return
whether good_weather is True or False at the time it's called.
"""
seconds = time.time()
seconds %= 10
if seconds <= 5:
good_weather = True
return good_weather
else:
good_weather = False
return good_weather
def start():
entries = 0
while entries < 4:
choice = raw_input("Hello! What do you want to do right now? Options: 1) Sleep, 2) Work, 3) Enjoy the great outdoors: ")
if choice == "1":
print "We are such stuff as dreams are made on, and our little life is rounded with a sleep. - Shakespeare, The Tempest"
elif choice == "2":
work()
elif choice == "3":
outdoors()
else:
print dontUnderstand
entries += 1
print done
def work():
entries = 0
entries2 = 0
while entries < 4:
choice = raw_input("Would you prefer sedentary office work or manual labor in the elements?: ")
if "office" in choice:
print "The brain is a wonderful organ; it starts working the moment you get up in the morning and does not stop until you get into the office. -Robert Frost"
elif "manual" in choice:
sunny = good_weather()
if sunny == True:
print "A hand that's dirty with honest labor is fit to shake with any neighbor. -Proverb"
else:
while entries2 < 3:
print rainedOut
entries2 += 1
print "You should probably just give up now."
sys.exit()
else:
print dontUnderstand
entries += 1
print done
sys.exit()
def outdoors():
sunny = good_weather()
if sunny == True:
print "Adopt the pose of nature; her secret is patience. -Ralph Waldo Emerson"
sys.exit()
else:
print rainedOut
start() # go back to start
start()
导入时间
导入系统
done=“我厌倦你了。再见。”
rainedOut=“对不起,雨挫败了你的计划:(”
dontUnderstand=“对不起,我不明白。”
好天气
“想象一个世界,每5秒钟就下雨一次(好天气=假),
然后又是晴天(good_weather=True)。此函数应返回
无论好天气是真是假,在它被称为。
"""
秒=时间。时间()
秒%=10
如果秒我想你想要这个部分:
else:
while entries2 < 3:
print rainedOut
entries2 += 1
print "You should probably just give up now."
sys.exit()
其他:
当entries2<3时:
打印rainedOut
离心率2+=1
打印“你现在应该放弃。”
sys.exit()
更像这样:
if entries2 < 3:
print rainedOut
entries2 += 1
continue # restart loop (optional)
else:
print "You should probably just give up now."
sys.exit()
如果entries2<3:
打印rainedOut
离心率2+=1
继续#重新启动循环(可选)
其他:
打印“你现在应该放弃。”
sys.exit()
您混淆了while
(循环)和if
(测试)功能。切勿使用sys.exit()
退出循环或函数。只需从函数中返回
。至于不应该返回的原因,请参阅此处的许多问题。如果这需要您将函数扩充为返回真/假
状态,或某个更复杂的对象/None,则需要对其进行扩充。您应该使用中断
从循环中退出,return
或return
从函数返回,您可以使用return
(只要您知道返回值将是什么)从函数内部的循环中快捷断开。不要使用sys.exit()
作为某种核喷枪来逃避所有控制流构造!(拆卸、内存泄漏、内务管理、消息日志记录、GUI线程、持久存储……这是一个糟糕习惯的许多原因中的一部分。此外,它会破坏单元测试/测试驱动设计)为变量赋值然后立即返回也没有意义。至少要执行两次(在good_weather
)中。改用return True
和return False
。@ReutSharabani:事实上,这六行都可以替换为return(秒),或者整个函数可以替换为return(time.time()%10)您应该向OP指出,整个if…elif…else
梯形图存在于while循环中。递增/更新循环变量的责任应该减少到该循环中的一个(或很少)点。OP对sys.exit()的无规飞溅使用
从循环中中断/从函数返回是一个坏的编码习惯,它们需要立即停止。虽然我非常同意,但这更多是为了“代码检查”而不是“堆栈溢出”。我已经把注意力集中在他的问题上。代码中还有更多的问题。我们可以在问题的注释中讨论这些问题。它是有效的!你是对的;谢谢。我感到困惑,因为我之前试图在work()函数中复制类似的功能(即,当条目<4…)我是否也需要将其从“while”改为“if”?如果对代码有任何其他注释——例如,如何代替sys.extit()——我洗耳恭听。我也是新来的,所以我不确定这是否是合适的地点?@mmrtre18:请参阅上面的注释,也可以在这里和CodeReview.stackexchange.com上阅读好的代码示例