-1

How to check the input result is integer or not in Python 3.8.2? I used this code but it is not working:

def get_ages():
    age = input('Please enter the age of members : ')
    age = recognize_input_character(age, 'get')
    return age

def recognize_input_character(characters, check):
    if check == 'get':
        if {characters} == int:
            print_results(characters, 'not_number')
            main_program(0)
        elif (characters < 10) or (characters > 90):
            print_results('input_failed', 'just_a_word')
            main_program(0)
        else:
            return int(characters)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • What did you expect that to do? Of course it's not working, it doesn't make any sense. `{characters} == int` compares a single-element set containing your string to the integer type by equality, it will **never** be true (unless you shadowed the name int with an equal set). – jonrsharpe Apr 14 '20 at 06:58
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – jonrsharpe Apr 14 '20 at 07:00
  • @BrunoVermeulen did you try your "solution" ? – bruno desthuilliers Apr 14 '20 at 07:18
  • @Teerth jain thanks a lot, I am newbie in python and coding I working on this problem several hours (just with if, while, for) and no chance to achieve to the right answer, I google several times and it can not help me too :) – Arian Khadem zadeh Apr 14 '20 at 11:44
  • If your code worked it wouldn't be necessary to ask, but your code not working isn't sufficient to justify it. Do research - what errors do you get? Actually learn the language, through structured tutorials, rather than [programming by coincidence](https://pragprog.com/the-pragmatic-programmer/extracts/coincidence). SO isn't a tutorial service, learn [ask]. – jonrsharpe Apr 14 '20 at 12:59
  • @jonrsharpe I am learning the language via some course not coincidence way, but in that code that I put here I changed the code you mention becouse someone told me it will work in the net!! anyway, you right I need to learn more and more about everything. thank you – Arian Khadem zadeh Apr 15 '20 at 06:37

1 Answers1

0

Here python always takes input and converts it to a String, Here we can check by this code

user_input = input ("Enter your Age")
try:
   val = int(user_input)
   print("Input is an integer number. Number = ", val)
except ValueError:
  try:
    val = float(user_input)
    print("Input is a float  number. Number = ", val)
  except ValueError:
      print("No.. input is not a number. It's a string")

This code simply checks if the input is a float, integer or a string....

Teerth jain
  • 75
  • 1
  • 1
  • 11
  • 1
    @BrunoVermeulen in Python3, `input()` always returns a string, so `isinstance()` is irrelevant. In Python2, you _definitly_ don't want to use `input()` (which `eval()` the user input - major security issue) but `raw_input()`, which always returns a string... – bruno desthuilliers Apr 14 '20 at 07:17