Skip to content Skip to sidebar Skip to footer

Python3 - Data Type Of Input()

I need to assign a user-provided integer value to an object. My format is as follows: object = input('Please enter an integer') The following print tests... print(type(object)

Solution 1:

while True:
 try:
  object = int(input("Please enter an integer"))
  break
 except ValueError:
  print("Invalid input.  Please try again")
print(type(object))
print(object)

Solution 2:

You can always catch a "traceback error" and substitute your own error handling.

user_input = input("Please enter an integer")
try:
    user_number = int(user_input)
except ValueError:
    print("You didn't enter an integer!")

Post a Comment for "Python3 - Data Type Of Input()"