Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python if statement inside for loop

I’m expecting to get "is null", because var0 is equal zero and the if statement shall print is null! Is is a syntax mistake?

var0 = 0
var1 = 1
var2 = 2

for i in range(3):
    if f"var{i}" == 0:
        print("is null")

I’m not getting any output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For future seekers, comment as answer:

var0 = 0
var1 = 1
var2 = 2

for i in range(3):
    if eval(f"var{i}") == 0:
        print("is null")

Result:

is null

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading