【发布时间】:2020-04-11 21:37:26
【问题描述】:
我是 python 新手,我正在努力学习我编写了这段代码
weight = input('How much do you weigh in pounds? ')
weight_kg = int(weight)* 0,453592
print (weight_kg + 'is your weight is kilogrammes')
但是当我运行它时,我得到了这个错误
Traceback (most recent call last):
File "C:/Users/hh/Desktop/PYTHON-LEARNING!/app.py", line 3, in <module>
print (weight_kg + 'hi')
TypeError: can only concatenate tuple (not "str") to tuple
【问题讨论】:
-
代码和回溯不匹配。
-
你已经在
weight_kg = ...,...行创建了一个带有逗号的元组。要创建float号码,请改用.! -
'0,453592',应该是'0.453592'
-
在尝试连接字符串时,您还必须将其转换为 str:
str(weight_kg) + '...'。 -
同样,一旦你完成了上述操作,你需要将
weight_kg转换为str,如果你想将它们连接在一起print (str(weight_kg) + ' is your weight is kilogrammes'),否则如果你不想隐式转换使用逗号而不是加号print (weight_kg, 'is your weight is kilogrammes')
标签: python python-3.x string tuples typeerror