程序忽略if​​语句,并在False时打印True [英] program ignoring if statement, and printing True when False

查看:94
本文介绍了程序忽略if​​语句,并在False时打印True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个家庭作业问题编写一个简单的程序,它似乎正在跳过我的if语句。我看过其他提出的问题,那里的问题似乎不是我的问题所以我希望你能提供帮助。

I am writing a simple program for a homework problem and It seems to be skipping my if statement. I have looked at other questions posed, and the problems there do not seem to be my problem so I am hoping you can help.

def isWordGuessed(secretWord, lettersGuessed):
if lettersGuessed == []:
    return False
else:
    for i in lettersGuessed:
        if i in secretWord == False:
            return False

        else:
            if i == lettersGuessed[-1]:
                return True

当我放入一些打印功能以查看它在做什么时,它会完全跳过

When I place in some print functions to see what it is doing, it completely skips

if i in secretWord == False:

我放右在这一行之上

print i in secretWord

它将打印出正确的布尔值,但即使打印出False,它仍然会跳过if语句。我觉得我必须忽略一些非常基本的东西,但是我似乎无法弄清楚它是什么,所以任何帮助都会受到赞赏。谢谢

and it will print out the correct Boolean, but even when it prints False, it still skips the if statement. I feel like I must be overlooking something pretty basic, but what it is I can't seem to figure out, so any help would be appreciated. Thanks

编辑:

以下是使用输入调用此函数的示例

Here is an example of a call to this function with inputs

isWordGuessed('apple',['a','e','i','k','p','r','s'])


推荐答案

        if i in secretWord == False:

这不符合您的想法。如果您希望在 i 不在 secretWord 中时采用此路径,则需要

This doesn't do what you think it does. If you want this path to be taken when i isn't in secretWord, you want

        if i not in secretWord:

您所拥有的链式比较。首先,它检查

What you have does a chained comparison. First, it checks

i in secretWord

如果这是真的,则检查

secretWord == False

如果这是真的(它不会是),那么如果 c $ c>路径。

If that's true (which it won't be), it then takes the if path.

这篇关于程序忽略if​​语句,并在False时打印True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆