【发布时间】: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