首页 > 解决方案 > 只能将 str (不是“列表”)连接到 str 被问及我无法解决问题

问题描述

我是python 2 天的新手,我收到一条错误消息can only concatenate str (not "list") to str,我似乎无法解决问题,请尽快提供反馈,所以如果你能给我一些帮助,我将非常感激 thx。

这是错误信息

Traceback (most recent call last):
  File "/Users/PycharmProjects/testA/app.py", line 57, in <module>
    print("                                                       " + characteristic)
TypeError: can only concatenate str (not "list") to str
import time

usern = input("Username: ")
print("Hi "+ usern + "!")
age = int(input("Year of birth: "))
age1 = 2020 - age
print(age1)
if 2020 - age < 15:
    print("Sorry " + usern + " you are to young.")
if 2020 - age > 15:
    print("Lets go " + usern + "!")
    print("                       /)")
    time.sleep(0.5)
    print("              /\___/\ ((")
    time.sleep(0.5)
    print("              \`@_@'/  ))")
    time.sleep(0.5)
    print("              {_:Y:.}_//")
    time.sleep(0.5)
    print("   ----------{_}^-'{_}----------")
    time.sleep(0.5)
    characteristic = []
    characteristic1 = input("Please Note Your Characteristics: ")
    characteristic.append(characteristic1)
    print(characteristic)
    characteristic2 = input ("Anything else[Y/N]")
    if characteristic2 == "N":
        print("ok")
    if characteristic2 == "Y":
        print("Ok")
        characteristic2 = input("So just type what else: ")
        characteristic.append(characteristic2)
        print(characteristic)
        [characteristic] = list
        time.sleep(10)
        phone = input("Please enter phone number: ")
    time.sleep(1)
    print("Loading")
    time.sleep(.1)
    print(".")
    time.sleep(0.25)
    print("..")
    time.sleep(0.25)
    print("...")
    time.sleep(0.25)
    print(".")
    time.sleep(0.25)
    print("..")
    time.sleep(0.25)
    print("...")
    time.sleep(5)
descr = input("Please write a small description about yourself and DO NOT PUT A FULL STOP OR AND PUNCTUATION WHEN YOURE DONE")
time.sleep(0.10)
print("                                                        " + usern)
time.sleep(0.10)
print("                                                             " + characteristic)
time.sleep(0.10)
print("                                                                    " + descr)
time.sleep(0.10)
print("                                                                      " + phone)
time.sleep(1)

标签: python

解决方案


欢迎使用 StackOverflow,欢迎使用 Python!

您已将特征定义为列表,然后将几个特征(字符串)附加到列表中。

所以你不能打印列表——把它想象成一个容器。您需要打开容器并打印容器中的每个项目。

我建议您将其命名为特征而不是特征。

然后你可以写

for each characteristic in characteristics:
    print(characteristic)

您可以使用特征 [0]、[1] 等访问列表中的各个特征。


推荐阅读