我对编程非常陌生。我有下面的问题,我试图列出旁边有数字的人的名字。所以在这里,我试图将“史蒂夫”加入“100”。我希望结果是 steve100。培训视频说您不能连接字符串和整数,因此您必须键入 str(grades[1]) 才能将 100 连接到 steve。但是,它对我不起作用,请参见下文。
>>> grades = ["steve", 100, "john", 50]
>>> grades[0] + grades[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> grades[0] + str(grades[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>>
最佳答案
您需要将 int 解析为字符串,并且您正在使用 str
函数正确执行此操作:
grades[0]+str(grades[1])
您收到错误 str object is not callable
可能是因为您之前定义了一个 str
变量。
你可以运行 del str
然后再试一次,它会成功 =)
关于python - STR 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013289/