Code error-- not able to figure out

Hi All,

I’m working on the following problem statement

I found the correct solution, but i’m curious about what went wrong with my solution

### Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line

And initially i wrote the following code, even though it didn’t threw any error, it was not showing any output

emp = []
i=2000
while(i<=3200):
*** if (i%7==0)and(i%5!=0):***
*** emp.append(i)***
*** i+=1 ***
print(emp)

Thanks in advance.

Regards
Ashish

It would be better if you properly formatted your code here. You can use the </> button in the editor to do so.

Currently, based on your code, it’s unclear how you have indented your code lines. So, can’t really say where the issue might be.

However, if the code doesn’t show any output and doesn’t throw an error, that can only mean that your code ran into an infinite loop situation.

And that would only happen if your code was structured as below -


emp = []
i=2000
while(i<=3200):
    if (i%7==0)and(i%5!=0):
        emp.append(i)
        i+=1
print(emp)

If your i+=1 code line is inside the if condition, then your i won’t get updated unless i is divisible by 7 and are not a multiple of 5. And if i does not get updated, your while loop will be always be True.

So, you will have to move i+=1 outside of your if condition for your code to work.

Your code is stuck in an infinite loop. Try below :

> emp = []
> i=2000
> while(i<=3200):
>     if (i%7 == 0) and (i%5!=0):
>         emp.append(i)
>         i+=1
>     else:
>         i+=1
> print(emp)

Hi

I got the solution, but i actually wanted to know what went wrong with my previous code. Which i pasted in the query.
Thanks for pinpointing the error. It was very helpful.
Have marked your reply as solution.

Regards
Ashish

1 Like

thanks for responding