无法使用基于布尔语句的while循环来正常运行

问题描述 投票:-2回答:3

我无法在布尔语句后面得到正确的逻辑,以便运行该函数。

我将我的代码基于LPTHW练习35.我基本上只是复制了该练习的结构并尝试使其工作。我想测试一下,看看我是否可以使while循环工作。

如果我用国会大厦Y键入是,则会打印“穿上雨衣,米......”,但它不会运行功能性降雨()。我收到以下错误:

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-condition退出程序,我明白了。但是,如果我希望它再次从顶部开始呢?它会循环,直到它得到是或否?


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.
python python-2.7 function while-loop
3个回答
0
投票

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


0
投票

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

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

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

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

虽然这解释了为什么没有调用rain(),你还需要注意你的if ... elif ...结构的顺序。如果输入'Yes',则始终在if语句后执行代码,并跳过后续条件。


0
投票

我将尝试更正代码并提供一些建议:

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永远不会被评估,只是因为如果下一个==“是”,那么第一个肯定会赢。我会把它改成:

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

“因为下雨=真,我怎么激活这个条件?”

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

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

缩进之后:声明。

好吧,你的其他声明将得到每个“否”输入,因为下雨确实总是真的。

认为雨布尔是某种方式需要:

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)
© www.soinside.com 2019 - 2024. All rights reserved.