gpt4 book ai didi

python - 为什么这段代码没有成功运行?

转载 作者:行者123 更新时间:2023-12-03 18:43:04 25 4
gpt4 key购买 nike

这是简单的代码,但我无法运行!
我的代码是:

name = input("What is your name? ")
print("My name is "+name+". "+"My name length is " + len(str(name)))
和输出是:
What is your name? Ali
Traceback (most recent call last):
File "c:\Users\iamAl\Desktop\Python\main.py", line 2, in <module>
print("My name is "+name+". "+"My name length is " + len(str(name)))
TypeError: can only concatenate str (not "int") to str

最佳答案

您需要转换从 len 返回的 int到 str , 不投 strstr然后使用 len

print("My name is " + name + ". " + "My name length is " + str(len(name)))

关于python - 为什么这段代码没有成功运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66156602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com