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

how to use while loop?

number = 50

while number >= 0:
    if number % 5 == 0:
        print(number)
        number = number - 1
    elif number % 5 == 1:
         continue
    number = number = number - 1      

the answer it’s giving me is 50 then the loop stops executing

Here is the program I am trying to make:

While Loops Practice #2
Create a While Loop that subtracts one by one the numbers from 50 to 0 (both numbers included) with the following additional conditions:

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

If the number is divisible by 5, show that number on the screen (remember that here you can use the modulus operation dividing by 5 and checking the remainder!)

If the number is not divisible by 5, continue executing the loop without showing the value on the screen (don’t forget to continue subtracting so that the program doesn’t run infinitely).

>Solution :

Here is your code, improved:

number = 50

while number >= 0:
    if number % 5 == 0:
        print(number)
    number = number - 1

I’m confused about that too, but here are my thoughts: you only subtracted when number % 5 == 1, but it can also be 2, 3, 4 or 5.

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