Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

Here is the python while loop code I am using:

i = 0.0

while i < 9.0:

    y = 13.0 - i

    if (y -i) == 6.0:

        print '[+] Solution found!'

        print 'x = ', i

        print 'y = ', y

        print 'z =', 8.0 - i

    i += 0.1

Even 'if' block never gets executed the condition is satisfied.

for example, if I put an increment of 0.5 i.e. i += 0.5 then the code works as it should. what is the issue?

1 Answer

0 votes
by (36.8k points)
edited by

Use the below Code :

i = 0.0

while i < 9.0:

    y = 13.0 - i

    if round((y -i),1) == 6.0:

        print('[+] Solution found!')

        print('x = ', round(i,1))

        print('y = ', round(y,1))

        print('z =', round(8.0 - i,1))

    i += 0.1

 Learn Python for Data Science Course to improve your technical knowledge.

Browse Categories

...