我对编程非常陌生。 我遇到了下面的问题,我正在试图用他旁边的号码列出此人的姓名。 所以在这里,我试图加入“史蒂夫”到“100”。 我希望结果是steve100。 训练视频说,你不能连接字符串和int,所以你必须键入str(等级[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 >>>I am extremely new to programming. I have the problem below, where I am trying to list the name of the person with a number next to him. So here, I am trying to join "steve" to "100". I want the outcome to be steve100. The training video says you can't concatenate strings and int's, so you have to type str(grades[1]) to get the 100 to concatenate to steve. However, it doesn't work for me, see below.
>>> 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然后再试一次,它会工作=)
You need to parse the int to string, and you are doing it correctly with the str function:
grades[0]+str(grades[1])You are getting the error str object is not callable probably because you have defined a str variable before.
You can run del str then try again, it will work =)
更多推荐
发布评论