【发布时间】:2019-08-29 09:20:13
【问题描述】:
我无法获得布尔语句背后的正确逻辑,以便函数运行。
我的代码基于 LPTHW 练习 35。我基本上只是复制了该练习的结构并尝试使其发挥作用。 我想测试一下是否可以使while循环正常工作。
如果我用大写字母 Y 输入 Yes,它会打印“穿上雨衣,m..”,但它不会运行函数 rain()。 我收到以下错误:
Traceback (most recent call last):
File "ex35_2.py", line 52, in <module>
temp ()
File "ex35_2.py", line 12, in temp
wool()
File "ex35_2.py", line 29, in wool
rain()
TypeError: 'bool' object is not callable
另外,else 条件退出程序,我明白了。但是,如果我想让它再次从顶部开始呢?它会一直循环直到它得到是或否?
def wool():
print "OK, so it is pretty cold outside!"
print "Put on the wool."
print "But is it raining?"
rain = True
while True:
next = raw_input("> ")
if next == "Yes": #if written with "Yes", it prints "Put on the.."
print "Put on the rain coat, m*!"
rain()
elif next == "Yes" and rain:
print "It is raining, but I dont wanna stress with the rain coat!"
elif next == "No" and not rain: #how do I activate this condition since rain = True?
print "You dont need a raincoat."
march("With wool and no raincoat.")
else:
print "You should make a choice." #this is the only output.
exit(0)
当我给其他用户输入时,它直接进入 else 语句。
You should make a choice.
【问题讨论】:
-
您的缩进不一致。请确保您显示的代码中的缩进是真实的。
-
在将其定义为布尔变量(使用
rain = True)之前的几行时,你怎么能调用rain?你也有一个叫做rain的函数(你没有给我们看)吗?这就是为什么创建minimal reproducible example 向我们展示如此重要的原因。另请阅读 how to ask good questions 和 this question checklist。
标签: python python-2.7 function while-loop