python - 这是正确的格式吗
问题描述
我的目标是找出这是否是正确的格式以找出类类型返回变量的数据类型类
我的预期结果是看到<class 'float'>
,但我得到了这个
这是我输入以获得结果<class 'float'>
但得到错误的代码print(float(type(a)))
这是我分配给变量的数字a
a*=2
Traceback (most recent call last):
File "C:\Webucator\assignment_operators.py", line 27, in <module>
print(float(type(a)))
TypeError: float() argument must be a string or a number, not 'type'
解决方案
您的功能顺序颠倒了。
该type(..)
函数返回传递对象的类型,在 Python 中是另一个对象,称为Type Object。打印类型对象会给你类似<class 'int'>
或<class 'float'>
取决于对象的东西。
从字符串或数字float(x)
返回浮点数。你不能将类型对象传递给,所以你不能这样做。float
float(type(a))
你需要float(a)
先在type()
.
>>> a = 1
>>> a*=2
>>> type(a)
<class 'int'>
>>> float(a)
2.0
>>> type(float(a))
<class 'float'>
推荐阅读
- java - 如何在 Querydsl 中实例化`PostgreSQLQuery`?
- javascript - 在我的 chrome 扩展程序和网站之间共享 cookie
- android - NoSuchMethodError ConstantExpressionEvaluator.evaluateExpression 添加jetpack compose时
- javascript - 为什么 Google Apps 脚本不允许我将我的范围称为变量?
- javascript - React 子组件从传入的内容接收不同的道具
- java - 为什么我的 TicTacToe 的 checkWinner 方法不起作用?
- spring-boot - OpenSource API Gateway 可以支持速率限制/月、API 密钥生成等 API 计划?
- regex - 将字符串中的多个数字提取到变量中
- java - org.nd4j.linalg.factory.Nd4j.zeros(长,长)
- amazon-web-services - 用于启动从 AWS ECR 到 AWS Elastic Beanstalk 的 docker 映像的 Bitbucket 管道