Python TypeError:在使用len()、input()和print()时,只能将str(而不是“int”)连接到str
由于下一个简单的代码对我有效:Python TypeError:在使用len()、input()和print()时,只能将str(而不是“int”)连接到str,python,Python,由于下一个简单的代码对我有效: print(len(input("¿whats your name? i will give you how many letters it has if you tell me: "))) 我试图通过编写一个更为“完整”的答案来让它更具想象力: 但这似乎不起作用 “输出”告诉我: 回溯(最近一次呼叫最后一次): 文件“main.py”,第7行,在 打印(“你的名字有”+len(输入(“你的名字是什么?如果你告诉我:)+”的话,我会告诉你它有
print(len(input("¿whats your name? i will give you how many letters it has if you tell me: ")))
我试图通过编写一个更为“完整”的答案来让它更具想象力:
但这似乎不起作用
“输出”告诉我:
回溯(最近一次呼叫最后一次):
文件“main.py”,第7行,在
打印(“你的名字有”+len(输入(“你的名字是什么?如果你告诉我:)+”的话,我会告诉你它有多少个字母)
TypeError:只能将str(而不是“int”)连接到str
len()
返回整数,您需要一个字符串来连接
尝试使用str()
将len转换为字符串,如下所示:
print("your name has " + str(len(input("whats your name? i will give tell you how many letters it has if you tell me: "))) + " in it")
print(f'your name has {len(input("What is your name? I will give tell you how many letters it has if you tell me: "))} letters in it.')
不能将整数(len返回的值)与前后的字符串连接起来,但可以将其放在f'字符串的花括号之间,如下所示:
print("your name has " + str(len(input("whats your name? i will give tell you how many letters it has if you tell me: "))) + " in it")
print(f'your name has {len(input("What is your name? I will give tell you how many letters it has if you tell me: "))} letters in it.')
为了便于将来参考,在发布问题之前,请尝试在谷歌上搜索您的错误。当您第一次学习编码时,您将遇到的99%的问题和错误都已在本网站上得到解答。就错误而言,之所以会出现此错误,是因为添加的字符串和整数与此类操作不兼容。