-2

I want to differentiate between the data types "string"&"int", if the input is "int" it will be appended, if not it will ask for correction

**no_inputs = 5
l =[]
while no_inputs >0:
    user_input = int(input("Please enter a number : "))
    if isinstance(user_input, int):        
        l.append(user_input)
        no_inputs -= 1
    elif isinstance(user_input, str):
        print("Please enter a number only ")
print(l)**
  • Please take the [tour] and read [what's on-topic](/help/on-topic) and [ask]. You must explicitly ask a _specific question_ based on what you have tried. – Pranav Hosangadi Jan 28 '21 at 22:29

1 Answers1

1

int() raises ValueError if input is not a number. Catch this exception to print user that input is not a number.

no_inputs = 5
l =[]
while no_inputs >0:
    try:
        user_input = int(input("Please enter a number : "))
        no_inputs -= 1
    except ValueError as e:
        print("Please enter a number only ")

print(l)
warchantua
  • 1,154
  • 1
  • 10
  • 24