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

1
  • 6
    A code checker gave a warning. That's not the same thing as an error, and Python is happy to execute the code with extra names you didn't actually use. Use the name _ or __ if you want to indicate that a variable is not going to be used. Commented Oct 13, 2018 at 12:36

4 Answers 4

25

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

0

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

1 Comment

Generally and especially here, one should avoid to disable pylints errors. The variable was effectively unused and it's way better to hide it using _
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.