【问题标题】:How to check Datatype with if else in Python?如何在 Python 中使用 if else 检查数据类型?
【发布时间】:2021-08-16 20:29:16
【问题描述】:

我想在 python 中使用if else检查我的数据类型

我想要的输出: 如果用户输入浮点数据类型,则打印It is float,或者如果用户输入另一个Datatype,则打印it is not float

代码:

c=(input("Enter the value\n"))
if type (c) == float:
    print('it is float')
else:
   print("it is not float")

我想要的输出:

Enter the value
12.1
it is float

我得到的输出:
如果我输入float datatype,它仍在打印it is else

Enter the value
12.1
it is else

【问题讨论】:

  • 我想你误会了。 input() 函数总是 返回一个str 类型的对象。如果您愿意,您可以自己将其转换为其他类型。
  • c 始终是str
  • 这能回答你的问题吗? How to check if input is float or int?
  • 那我该怎么办?如果我使用c=float(input("Enter a number"),它总是显示浮动。有什么解决办法吗?

标签: python python-3.x


【解决方案1】:
c=input("Enter the value\n")
try:
    float(c)
    print('it is float')
except ValueError:
   print("it is not float")

您的问题是输入总是给您 str 值,因此通过尝试将其转换为浮点数,您知道它是否为浮点数

【讨论】:

    【解决方案2】:

    你应该有这样的东西

    c = (input("Enter the value\n"))
    try:
        f = float(c)
        is_float = True
    except ValueError:
        is_float = False
    

    【讨论】:

      【解决方案3】:

      检查对象是 int 还是 float:isinstance()

      可以通过内置函数type()获取对象的类型。

      i = 100
      f = 1.23
      
      print(type(i))
      print(type(f))
      # <class 'int'>
      # <class 'float'>
      

      内置函数isinstance(object, type)可以用来判断一个对象是否属于特定类型。

      在 Python 中获取/确定对象的类型:type()isinstance()

      print(isinstance(i, int))
      # True
      
      print(isinstance(i, float))
      # False
      
      print(isinstance(f, int))
      # False
      
      print(isinstance(f, float))
      # True
      

      在这种情况下,由于只检查类型,因此无法确定float的值是否为整数(小数部分为0)。

      f_i = 100.0
      
      print(type(f_i))
      # <class 'float'>
      
      print(isinstance(f_i, int))
      # False
      
      print(isinstance(f_i, float))
      # True
      source: check_int_float.py
      Check if float is integer: is_integer()
      float has is_integer() method that returns True if the value is an integer, and False otherwise.
      
      f = 1.23
      
      print(f.is_integer())
      # False
      
      f_i = 100.0
      
      print(f_i.is_integer())
      # True
      

      例如,对于整数(int 或整数浮点数)返回 True 的函数可以定义如下。此函数为 str 返回 False。

      def is_integer_num(n):
          if isinstance(n, int):
              return True
          if isinstance(n, float):
              return n.is_integer()
          return False
      
      print(is_integer_num(100))
      # True
      
      print(is_integer_num(1.23))
      # False
      
      print(is_integer_num(100.0))
      # True
      
      print(is_integer_num('100'))
      # False
      

      致谢:https://note.nkmk.me/en/python-check-int-float/

      【讨论】:

      • 绝对应该使用isinstance()而不是检查type()的返回。谢谢你提出来。然而,在这种情况下,input() 总是返回一个字符串,所以我们不能真正以我们希望的方式使用isinstance(),并且强制转换为 float 可能会引发错误,所以我们真的需要一个围绕 try: 构建的答案
      猜你喜欢
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      相关资源
      最近更新 更多