Meisternoob
Meisternoob

Reputation: 171

Python for loop ending with no criteria satisfied

I have this program that asks the user for two numbers and checks if every number in between the numbers can be divided with 5 and 7. I would like the program to output something like "No number found" if no number in the range can be divided with 5 and 7. for example if the user entered 3 and 4 it would print "No number found"

I have tried a couple of different things but none are working.

start = int(input("Start: "))
stop = int(input("Stop: "))


for number in range(start, (stop+1),1):
    if number % 5 == 0 and number % 7 ==0:
        print("Number", number, "can be divided with 5 and 7")
        print("Stop search")
        break
    elif  number % 5 == 0 and number % 7 !=0:
        print(number, "can't be divided with 7, next.")
    elif number % 5 != 0:
        print(number,"can't be divided with 5, next.")

Upvotes: 3

Views: 82

Answers (4)

lmiguelvargasf
lmiguelvargasf

Reputation: 69963

You can also use a variable as a flag that tells you whether or not the number was found:

found = False
for number in range(start, (stop+1),1):
    if number % 5 == 0 and number % 7 ==0:
        print("Number", number, "can be divided with 5 and 7")
        print("Stop search")
        found = True
        break
    elif  number % 5 == 0 and number % 7 !=0:
        print(number, "can't be divided with 7, next.")
    elif number % 5 != 0:
        print(number,"can't be divided with 5, next.")

if not found:
    print("No number that can be divided by both 5 and 7 found.")

Upvotes: 0

Colin G.
Colin G.

Reputation: 41

Add a boolean condition for the for loop.

found = False

Then change this boolean to true when the stopping condition of the for loop happens.

Finally, perform an action based on the found boolean condition like:

if !found:
    # Print when the value was not found.

[edit]: The solution posted by RemcoGerlich is the better solution.

Upvotes: 0

JTateCC
JTateCC

Reputation: 56

As Daniel Roseman said I was also unaware of the else clause so I did a hack method of using a test variable

start = int(input("Start: "))
stop = int(input("Stop: "))

Test = 0
for number in range(start, (stop+1),1):
    if number % 5 == 0 and number % 7 ==0:
        print("Number", number, "can be divided with 5 and 7")
        print("Stop search")
        Test = 1
        break
    elif  number % 5 == 0 and number % 7 !=0:
        print(number, "can't be divided with 7, next.")
    elif number % 5 != 0:
        print(number,"can't be divided with 5, next.")


if Test == 0:
    print("no number in range")

Upvotes: 0

RemcoGerlich
RemcoGerlich

Reputation: 31270

You can use an else: clause with for loops. It is only executed when the whole for loop ends normally and no break clause was reached. You have a rare case where it is useful!

for number in range(start, (stop+1),1):
    if number % 5 == 0 and number % 7 ==0:
        print("Number", number, "can be divided with 5 and 7")
        print("Stop search")
        break
    elif  number % 5 == 0 and number % 7 !=0:
        print(number, "can't be divided with 7, next.")
    elif number % 5 != 0:
        print(number,"can't be divided with 5, next.")
else:
    print("No number that can be divided by both 5 and 7 found.")

Upvotes: 7

Related Questions