Jamiu NINIOLA
Jamiu NINIOLA

Reputation: 131

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:

W0612: Unused variable 'i'

But when I run the same program with the built in python 'Idle' interpreter, the program runs just fine.

Upvotes: 10

Views: 11768

Answers (4)

shaik moeed
shaik moeed

Reputation: 5785

As you are not using 'i' in for loop. 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)

Assign unnecessary variable values to _. While _ is still a standard identifier, it's commonly used to indicate non-essential variable content for tools(i.e., VSCode in your case) and human readers, preventing warnings about its unused status.

Upvotes: 25

Mohsin Mahmood
Mohsin Mahmood

Reputation: 3436

Use the following command to generate a pylint rc file with all its options present

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

Upvotes: 0

Joe Iddon
Joe Iddon

Reputation: 20424

This is just your IDE alerting you that you have a variable defined, i, which you are not using.

Python doesn't care about this as the code runs fine, so the interpreter doesn't throw any error as there is no error!

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.

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.

Upvotes: 5

kaan bobac
kaan bobac

Reputation: 747

This is about ignoring pylint warnings.

1st way:

adding #pylint: disable=unused-argument before for loop

2nd way: you can ignore them from pylint config file

[MESSAGES CONTROL]
disable=unused-argument

Upvotes: 0

Related Questions