Skip to main content
added 171 characters in body
Source Link
blhsing
  • 109.1k
  • 9
  • 88
  • 132

You can use ast.literal_eval() instead to parse the string returned by the input() function into an object represented by the content of the string, so that you can use isinstance() to test its type as you intended:

import ast
while True:
    try:
        valor = ast.literal_eval(input("Give me a number: "))
        break
    except SyntaxError, ValueError:
        print("Please enter a valid number.")

You can use ast.literal_eval() instead to parse the string returned by the input() function into an object represented by the content of the string, so that you can use isinstance() to test its type as you intended:

import ast
valor = ast.literal_eval(input())

You can use ast.literal_eval() instead to parse the string returned by the input() function into an object represented by the content of the string, so that you can use isinstance() to test its type as you intended:

import ast
while True:
    try:
        valor = ast.literal_eval(input("Give me a number: "))
        break
    except SyntaxError, ValueError:
        print("Please enter a valid number.")
Source Link
blhsing
  • 109.1k
  • 9
  • 88
  • 132

You can use ast.literal_eval() instead to parse the string returned by the input() function into an object represented by the content of the string, so that you can use isinstance() to test its type as you intended:

import ast
valor = ast.literal_eval(input())