Home > database >  How can I check if number is multiple of 3 or contains the digit 5?
How can I check if number is multiple of 3 or contains the digit 5?

Time:09-27

digit = 5
for i in range(1, 101):
    if i % 3 == 0:
        print ("MultipleOfThree", end=" ")
    elif i:
        for digit in str(i):
            print ("FiveInIt", end=" ")
    else:
        print(i, end=" ")

I want to see in one line like that:

1 2 MultipleOfThree 4 FiveInIt MultipleOfThree 7 8 MultipleOfThree 10 11 MultipleOfThree 12 13 14 FiveInIt ........ 

But the output gives:

FiveInIt FiveInIt MultipleOfThree FiveInIt FiveInIt MultipleOfThree .......

CodePudding user response:

It should be:

digit = 5
for i in range(1, 101):
    if i % 3 == 0:
        print ("MultipleOfThree", end=" ")
    elif str(digit) in str(i):
        print ("FiveInIt", end=" ")
    else:
        print(i, end=" ")

CodePudding user response:

This should work

digit = '5'
for i in range(1, 101):
    if i % 3 == 0:
        print("MultipleOfThree", end=" ")
    elif digit in str(i):
        print("FiveInIt", end=" ")
    else:
        print(i, end=" ")
  • Related