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

I want the program to immediately end the current iteration of the loop and start a new one if I enter a negative number

num=int(input('Enter a number:'))
while num !=0:
    if num>0:
        if num%2==0 and num%7==0:
            print('The number  is a multiple of two and seven.' )
            num=int(input('Enter a number:'))
        elif num%2==0:
            print('The number  is a multiple of two.' )
            num=int(input('Enter a number:'))
        elif num%7==0:
            print('The number  is a multiple of seven.' )
            num=int(input('Enter a number:'))
        else:
            print('The number  is not a multiple of two or seven.' )
            num=int(input('Enter a number:'))
    else:
        continue   
else:
 print('The user did not enter negative numbers.' )

Answer:

Enter a number:
>>> 5
The number  is not a multiple of two or seven.
Enter a number:
>>> 6
The number  is a multiple of two.
Enter a number:
>>> -8

>Solution :

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

The question is not very clear but I think I understand what you’d like to achieve.

Your solution is partially correct, but the "continue" inside the else is not followed by a new insertion of "num". So the input "num" is never changed when you insert a negative number.

Also the While-else clause has no sense in this case, if you want to count the negative number inserted you can just put a counter inside the first else clause.

You should change the code and move the "input" inside the while:

num = 1
negative_counter = 0
while num !=0:
    num=int(input('Enter a number:'))
    if num>0:
        if num%2==0 and num%7==0:
            print('The number  is a multiple of two and seven.' )
        elif num%2==0:
            print('The number  is a multiple of two.' )
        elif num%7==0:
            print('The number  is a multiple of seven.')
        else:
            print('The number  is not a multiple of two or seven.' )
    else: 
        negative_counter = negative_counter + 1
        continue   
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