-1

I try to make a function that at first will check if the input is not a string. But if the user inputs a float, it gets False. I need it to accept both Int and Float, bot not a string.

def squat():
    value = input("What is your RM1?")

    if value.isnumeric():
        rm1 = float(value)
        print("Your RM1 is: ", rm1)
        print(type(value))

    else:
        print("Error")


squat()
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • 4
    `input` always returns a string – roganjosh Sep 03 '18 at 10:54
  • Did you mean to ask a way to determine if the users input was a _numerical_ string? If so you can refer to an existing question https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number – Rajeev Atmakuri Sep 03 '18 at 10:59

2 Answers2

1

You may use a try..except block in your case

def squat():
    value = input("What is your RM1?")
    try:
         rm1 = float(value)
    except ValueError:
         print("error")
         exit(1)
abc
  • 11,579
  • 2
  • 26
  • 51
0

The input() function always returns a string. You can use a regex to check if the string looks like number:

import re

value = input('What is your RM1? ')
if re.match(r'^\d+(\.\d+)?$', value):
    rm1 = float(value)
    print('Your RM1 is: ', rm1)
else:
    print('Error')
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378