【问题标题】:How to check is an input is a float of not [duplicate]如何检查输入是否为非浮点数[重复]
【发布时间】:2017-04-10 16:20:20
【问题描述】:

我目前正在编写一些代码来判断我的用户输入是浮点数还是整数,但它似乎不起作用,谁能帮忙?

 number = input ("Enter something:")
 if type(number) != int:  
      print("Isn't a whole number")
 else:
      print("is a whole number")

【问题讨论】:

  • 改成if not type(number) is int
  • @kuro: number always 不会是字符串吗?您不需要尝试使用int()float() 进行转换吗?
  • @Rocket Hazmat 我认为这对 python 3.x 来说是正确的

标签: python if-statement integer


【解决方案1】:

使用is 进行类型检查,即 not type(a) is int

【讨论】:

  • 你也可以type(a) is not int.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
相关资源
最近更新 更多