我正在尝试创建一个程序,要求用户输入他/她的名字并将输入记录到列表中(正在努力进入字典,但似乎我做了一个嘘声!)但它返回&# 34; TypeError:只能将列表(不是" str")连接到列表"。以下是代码。提前谢谢。
namedic = []
while True:
print ("Please, enter your name:")
name = input()
if len(name) > 3:
print ("Welcome")
else:
print ("Ew, your name have less than 4 letters! Gross! Try a new one")
continue
namedic = namedic + name
print ("Ah, your name have at least 4 words, good name.")
for name in namedic:
print (name)
答案 0 :(得分:0)
如错误所示,+
运算符不用于将元素追加到列表中。您可以使用append
方法:
namedic.append(name)
答案 1 :(得分:0)
#your code should rather be like this;
namedic = []
while True:
print ("Please, enter your name:")
name = input()
if len(name) > 3:
print ("Welcome")
else:
print ("Ew, your name have less than 4 letters! Gross! Try a new one")
continue
namedic.append(name)
print ("Ah, your name have at least 4 words, good name.")
for name in namedic:
print (name)