Home > Software engineering >  How do I find out what string the user tried to input into a integer function?
How do I find out what string the user tried to input into a integer function?

Time:05-03

How do I get this program to work? If the user inputs a letter or word into the input function, instead of an integer the program should print "this is not a number". But if the user inputs "stop" instead of the integer, the program is supposed to stop. My code looks like this:

try:
    inp = int(input("number:"))
    print(inp)
except:
    if inp == "I don't know":
        print("learn math")
    else:
        print("This is not a number!")

CodePudding user response:

number = input("number: ")

def is_integer(number):
    try:
        int(number)
        print("learn math")
    except ValueError:
        print("This is not a number!")
is_integer(number)

CodePudding user response:

while True:
   string=input('enter values :')
   if string=='stop':
        break
   elif string=="I don't know":
       print('learn math')
   if string.isalpha:
       print('Not a number !')

CodePudding user response:

Taking input in one line and converting it to int in another line should work.

try:
    inp = input("number:")
    inp = int(inp)
    print(inp)
except:
    if inp == "I don't know":
        print("learn math")
    else:
        print("This is not a number!")

YW!

  • Related