Home > Software design >  Checking if an integer contains a certain digit in a for loop
Checking if an integer contains a certain digit in a for loop

Time:02-08

I am trying to solve this problem:
Write a code which computes the sum = 1/1 ... 1/6 1/8 ... 1/16 ... from 1 to 10000 ,skipping over the integers that contain the number 7.

Here is my code so far, I am stuck on trying to skip over the integers that contain the digit 7

def int():
 sum=0
 for i in range(10000):
    if # integer contains digit==7:
        continue
    sum  = 1/i
 return sum

CodePudding user response:

def int():

    sum=0
    for i in range(1,10000): # make sure range is from 1 to whatever. Otherwise, you'll a ZeroDivisionError
        if '7' in str(i): #convert i to string
            continue
        sum  = 1/i
    return sum
print(int())

CodePudding user response:

if you want to check a digit in number use this

if "7" in str(i)
  •  Tags:  
  • Related