简体   繁体   中英

print all number divisible by 7 and contain 7 from 0 to 100

I am trying to make a for loop that will print all the numbers divisible by 7 from 0 to 100 and also numbers that contain 7, like this example: 7 14 17 21 27 28 and so on

I tried to do this:

for i in range(101):
    if i%7==0 or i==range(7,98,10):
        print(i)

but it prints this:

7 14 21 and so on

what should I do?

As pointed out elsewhere, i == range(7,98,10) doesn't really make sense, as you can't compare an integer to a range object. What you're actually trying to do is a membership test, which in python uses the in keyword: i in range(7,98,10)

The next issue you'll find is that you're missing a class of numbers that contain the digit 7. Namely, the 70-79 range (although you'll get 70 and 77 from the other rules). Following your current strategy, your final answer might look like this:

for i in range(101):
    if i % 7 == 0 or i in range(7, 98, 10) or i in range(70, 80):
        print(i)

This is perfectly valid. You can also approach it mathematically, by noting that the numbers with a 7 ones digit will have a remainder of 7 when divided by 10 (known as the modulo operator, % ), and numbers with a 7 tens digit will have a quotient of 7 when divided by 10 (using integer division, // ).

for i in range(101):
    if i % 7 == 0 or i % 10 == 7 or i // 10 == 7:
        print(i)

Finally, there's a neat trick you can do since you're checking both division and mod by the same number; these two results will be given at the same time by the built-in divmod() function. You just need to check if either of them are 7. (Note that this won't work if you expand beyond 2 digit numbers.)

for i in range(101):
    if i % 7 == 0 or 7 in divmod(i, 10):
        print(i)

(Note: I left the range of the for loop the same as your sample code. This will include 0 in the output, since it is divisible by 7. If you don't want 0 , just change the range to range(1, 101) .)

well actually when you do:

i == range(7, 98, 10)

you are comparing an integer with a iterable. So this condition always evaluates to False .


What you want to do is to check whether the string representation of you number contains 7 . You can do:

'7' in str(i)

So globally, you could write the following:

for i in range(0, 101):
    if i % 7 == 0 or '7' in str(i):
        print i

if you don't want to do '7' in number and mathematically do it here is how you can

n = 7
i = 0
j = 100

# largest_multiple_of_n_smaller_than_j = (j - j%n) # 100 - 100%7 --> 100 -2 --> 98
# 98//7 --> 14 
# therefore we can say 7 multiply i( where i goes from 0 --> 14) will be less than equal to 100

multiples_of_n = [n*i for i in range(0, ((j - j%n) // n) +1)] # 0 7 14 21 ....98

# 17 27 37..(dont pick 77 here)..97 and 71 72 73...77..79
contains_n = [i*10+n for i in range(1,10) if i != n] + [n*10+x  for x in range(1,10) if (n*10+x) % n !=0] 

'''
or use this 

contains_n = [x for x in range(10+n,j,10) if x != n*11] + [x for x in range(n*10+1,n*10+10) if x % n !=0] 
'''

mult_and_contain_n =  sorted(multiples_of_n + contains_n)
print(mult_and_contain_n)


Output

n = 7

[0, 7, 14, 17, 21, 27, 28, 35, 37, 42, 47, 49, 56, 57, 63, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 84, 87, 91, 97, 98]


n = 5

[0, 5, 10, 15, 15, 20, 25, 25, 30, 35, 35, 40, 45, 45, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 65, 65, 70, 75, 75, 80, 85, 85, 90, 95, 95, 100]

Using list comprehension and using not i % 7 along with the checking str(i) contents for a 7

print([i for i in range(7, 101) if not i % 7 or '7' in str(i)])
# [7, 14, 17, 21, 27, 28, 35, 37, 42, 47, 49, 56, 57, 63, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 84, 87, 91, 97, 98]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM