Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 985
Jamiu NINIOLA Avatar asked Oct 13 '18 12:10

Jamiu NINIOLA


2 Answers

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)
like image 97
shaik moeed Avatar answered Oct 14 '22 10:10

shaik moeed


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.

like image 33
Joe Iddon Avatar answered Oct 14 '22 10:10

Joe Iddon