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

Question:

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
Asked By: Luna X

||

Answers:

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   
Answered By: Roberto Ciardi
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.