python - 不断收到类型错误:+ 的不支持的操作数类型:python 中的“int”和“str”
问题描述
开始上 Python 课程,并被要求调试一个小程序。除了这一部分之外都完成了
a = 2 # Declare a variable with a value, 2
b = 19 # Declare a variable with a value, 19
c = a + (".") + b # Concatanate strings, a, ".", and b
print (type(c)) # print c's type
print (c) # print c
预期产出
class<str>
2.19
无法计算出这个小数点,我知道它与此有关。
解决方案
a
并且b
是 int 类型。您必须将它们更改为字符串才能进行连接:
a = 2
b = 19
c = str(a) + "." + str(b)
print (type(c))
print (c)
推荐阅读
- php - 根据准备好的语句的结果分配对象属性
- html - 如何使“下一步”按钮在 HTML 中工作?
- ios - 异步块中 Any 与 Void 的返回之间的区别
- html - 如何在不使用 html 标签的情况下更改单词的颜色?
- push-notification - 在 FCM 中处理延迟的未注册令牌
- python - 如何使用 Selenium 在元素的父元素中查找元素
- google-bigquery - Apache Beam Java 2.26.0:BigQueryIO '请求中没有行'
- delphi - 如何在 Delphi 的 TcxDbDateEdit 上仅显示月/年?
- android - 为什么 Jetpack Compose 不将按钮中的文本大写?
- blazor - 如何为 Blazor WebAssembly 和 IdentityServer 4 配置持久登录?