heather - 2 months ago 4x
Python Question

# Iterating variable when if statement comes up false

I wrote this program in python:

``````num=51

if (num % 3 == 1):
if (num%4 == 2):
if (num%5 == 3):
if (num%6 ==4):
print num
else:
print "not right number, try again - error 1"
else:
print "not right number, try again - error 2"
else:
print "not right number, try again - error 3"
else:
print "not right number, try again - error 4"
``````

Which works well, except I really don't want to have to hand iterate
`num`
until I get the answer I want (I wrote this to solve a mathematics problem I wanted to solve - this is not homework, though). If anyone could detail to change all of the
`else`
statements to add a statement incrementing
`num`
by one and return to the beginning of the for loop, that'd be great.

Thanks!

You can use the `break` statement to terminate the loop

``````num=1

while True:
if (num % 3 == 1):
if (num%4 == 2):
if (num%5 == 3):
if (num%6 ==4):
print num
break
else:
print "not right number, try again - error 1"
else:
print "not right number, try again - error 2"
else:
print "not right number, try again - error 3"
else:
print "not right number, try again - error 4"
num += 1
``````