我无法在布尔语句后得到正确的逻辑,因此该函数无法运行。
我将代码基于LPTHW练习35。我基本上只是复制了该练习的结构,并试图使其生效。 我想测试一下,看看是否可以使while循环正常工作。
如果我在国会大厦Y上键入“是”,它将打印“放在雨衣上,..”,但不会运行函数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.
答案 0 :(得分:0)
我假设您在调用rain()
函数之前在某个位置加载了模块或定义了函数wool()
。
所以,现在发生的事情是,在声明变量rain = True
的那一刻本地变量定义优先于先前的函数定义。这意味着,而不是调用函数rain()
而是“调用”变量rain
。
为了避免这种情况,请确保跟踪您的名称空间。如果导入包含rain
的模块,则可以导入整个模块,然后使用module_name.rain()
。
如果仅在脚本中定义函数,则需要重命名函数或变量。例如,将rain = True
替换为it_is_raining = True
虽然这解释了为什么未调用rain()
的原因,但是您还需要注意if ... elif ...
构造的顺序。如果输入'Yes'
,则将始终在if语句之后执行代码,并且随后的条件将被跳过。
答案 1 :(得分:0)
Your program prints out the else-statement, because you haven't indented the content inside your else and elif. Right now, "You should make a choice" is printed outside your if-statement.
答案 2 :(得分:0)
I will try to correct the code and give some advice:
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!"
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)
Look for #!!! First one: this elif will never be evaluated, just because if next == "Yes" the first if will win, surely. I would change it to:
if next == "Yes": #if written with "Yes", it prints "Put on the.."
if rain:
print "It is raining, but I dont wanna stress with the rain coat!"
else:
print "Put on the rain coat, *****!"
rain()
elif next .....
Logic behind something here is hidden: you are asking if it's raining, and write down: rain = True
"how do I activate this condition since rain = True?"
well, if user said it's not raining, it's not raining.
I dunno why you are using a pre-setted boolean even if you have user input.
Indentation after else: statement.
Well, you else statement will get every "No" input, because rain is indeed always True.
Thinking that rain boolean is somehow needed:
elif next == "No"
if rain:
print("Lie!")
#maybe rain = False ??
else:
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)