-5

I want to create a function that can see if an input contains an int in some way.

For example, if the input was 17, 7, 70, 87 etc. It could check if a seven is present. Like the number 17 contains a 7.

Code:

UserInput = int(input("Input a number and check if it contains 7"))
if (insert statement to check for 7):
    print("there is a seven in your number") 

Thank you in advance.

Otto
  • 19
  • 8

2 Answers2

1

Since OP asked for a function, it would look something like this:

def checkNum(num):
    if "7" in str(num):
        return True
    else:
        return False

The (num) variable is the information that is passed into the function. In this case, it's your given number. The if statement does pretty much what it says it does. It checks if "7" is in the data that you inputted. If it is, it returns the value of True. If that isn't true, it returns False.

Finally you'll need a statement to print the result of the function and to input your data like this:

print(checkNum(*your number here*))

You can use input instead of a number to type in the number if you'd like.

bsukalo
  • 66
  • 5
1

Here's a funny way of doing it

if len(input("Input a number and check if it contains 7").split('7'))>1:
print("there is a seven in your number")
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18