简体   繁体   English

for循环中未使用的变量

[英]Unused variable in a for loop

def main():
    print ("This program illustrates a chaotic function")
    x = (float(input("Enter a number between 0 and 1: ")))
    for i in range(10):
        x = 3.9 * x * (1-x)
        print (x)

main()

When I type the program above into my Visual Studio Code desktop application, it returns the problem notification:当我在我的 Visual Studio Code 桌面应用程序中键入上面的程序时,它返回问题通知:

W0612: Unused variable 'i'

But when I run the same program with the built in python 'Idle' interpreter, the program runs just fine.但是当我使用内置的 python '空闲'解释器运行相同的程序时,程序运行得很好。

As you are not using ' i ' in for loop.因为您没有在 for 循环中使用 ' i '。 Change it ' _ ' as shown in below将其更改为“ _ ”,如下所示

def main():
    print ("This program illustrates a chaotic function")
    x = (float(input("Enter a number between 0 and 1: ")))
    for _ in range(10):
        x = 3.9 * x * (1-x)
        print (x)

This is just your IDE alerting you that you have a variable defined, i , which you are not using.这只是您的 IDE 提醒您您定义了一个变量i ,但您没有使用它。

Python doesn't care about this as the code runs fine, so the interpreter doesn't throw any error as there is no error! Python 不关心这个,因为代码运行良好,所以解释器不会抛出任何错误,因为没有错误!

There isn't really much more to say, Visual Studio is just notifying you in case you meant to use i at some point but forgot.没有什么可说的了,Visual Studio 只是通知您,以防您在某个时候打算使用i但忘记了。

It is Python convention to use an underscore as the variable name when you are just using it as a placeholder, so I'd assume Visual Studio would recognise this and not notify you if you used _ instead of i .当您仅将下划线用作占位符时,使用下划线作为变量名是 Python 约定,因此我假设 Visual Studio 会识别出这一点,并且如果您使用_而不是i ,则不会通知您。

This is about ignoring pylint warnings.这是关于忽略 pylint 警告。

1st way:第一种方式:

adding #pylint: disable=unused-argument before for loop在 for 循环之前添加#pylint: disable=unused-argument

2nd way: you can ignore them from pylint config file第二种方式:您可以从 pylint 配置文件中忽略它们

[MESSAGES CONTROL]
disable=unused-argument

使用以下命令生成包含所有选项的 pylint rc 文件

pylint --generate-rcfile > $HOME/.pylintrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM