【问题标题】:Can't get the while loop based on boolean statements to run properly无法使基于布尔语句的 while 循环正常运行
【发布时间】: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 questionsthis question checklist

标签: python python-2.7 function while-loop


【解决方案1】:

我会尝试更正代码并给出一些建议:

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)

寻找#!!! 第一个:这个 elif 永远不会被评估,因为如果 next == "Yes" 第一个 if 肯定会赢。我会将其更改为:

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 .....

这背后的逻辑是隐藏的:你问是否在下雨,并写下:rain = True

“既然rain = True,我该如何激活这个条件?”

好吧,如果用户说没有下雨,那就没有下雨。

即使您有用户输入,我也不知道您为什么要使用预先设置的布尔值。

else 后面的缩进:语句。

好吧,你的 else 语句将得到每个“否”输入,因为 rain 确实总是 True。

认为在某种程度上需要雨布尔值:

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)

【讨论】:

    【解决方案2】:

    我假设您在调用wool() 函数之前在某处加载了一个模块或定义了函数rain()

    所以,现在发生的事情是,在您声明变量rain = True 的那一刻局部变量定义优先于您之前的函数定义。这意味着,您“调用”变量rain,而不是调用函数rain()

    为了避免这种情况,请务必跟踪您的命名空间。如果导入包含rain的模块,可以导入整个模块,然后使用module_name.rain()

    如果您只是在脚本中定义函数,则需要重命名函数或变量。例如将rain = True 替换为it_is_raining = True

    虽然这解释了为什么不调用 rain(),但您还需要注意 if ... elif ... 构造的顺序。如果输入'Yes',就会一直执行if语句后面的代码,后面的条件就跳过了。

    【讨论】:

      【解决方案3】:

      您的程序打印出 else 语句,因为您没有缩进 else 和 elif 中的内容。现在,“你应该做出选择”打印在你的 if 语句之外。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-17
        • 2013-06-17
        • 1970-01-01
        • 1970-01-01
        • 2015-03-14
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多