STR功能不起作用(STR function not working)

编程入门 行业动态 更新时间:2024-10-25 21:18:53
STR功能不起作用(STR function not working)

我对编程非常陌生。 我遇到了下面的问题,我正在试图用他旁边的号码列出此人的姓名。 所以在这里,我试图加入“史蒂夫”到“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 =)

更多推荐

本文发布于:2023-08-07 01:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   功能   STR   function   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!