Python 类型错误:只能将列表(不是“str”)连接到列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19398993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: can only concatenate list (not "str") to list
提问by user2885647
I am trying to make an inventory program to use in an RPG game. The program needs to be able to add and remove things and add them to a list. This is what I have so far:
我正在尝试制作一个用于 RPG 游戏的库存程序。该程序需要能够添加和删除事物并将它们添加到列表中。这是我到目前为止:
inventory=["sword","potion","armour","bow"]
print(inventory)
print("\ncommands: use (remove) and pickup (add)")
selection=input("choose a command [use/pickup]")
if selection=="use":
print(inventory)
remove=input("What do you want to use? ")
inventory.remove(remove)
print(inventory)
elif selection=="pickup":
print(inventory)
add=input("What do you want to pickup? ")
newinv=inventory+str(add)
print(newinv)
When I run this and try to pick some thing up I get this error:
当我运行它并尝试选择一些东西时,我收到此错误:
Traceback (most recent call last):
File "H:/Year 10/Computing/A453/Python Programs/inventory.py", line 15, in <module>
newinv=inventory+str(add)
TypeError: can only concatenate list (not "str") to list
Does any one have a fix for this, would be greatly appreciated
有没有人解决这个问题,将不胜感激
采纳答案by jabaldonedo
I think what you want to do is add a new item to your list, so you have change the line newinv=inventory+str(add)
with this one:
我认为你想要做的是在你的列表中添加一个新项目,所以你已经改变了这一行newinv=inventory+str(add)
:
newinv = inventory.append(add)
What you are doing now is trying to concatenate a list with a string which is an invalid operation in Python.
您现在正在做的是尝试将列表与字符串连接起来,这在 Python 中是无效的操作。
However I think what you want is to add and delete items from a list, in that case your if/else block should be:
但是我认为您想要的是从列表中添加和删除项目,在这种情况下,您的 if/else 块应该是:
if selection=="use":
print(inventory)
remove=input("What do you want to use? ")
inventory.remove(remove)
print(inventory)
elif selection=="pickup":
print(inventory)
add=input("What do you want to pickup? ")
inventory.append(add)
print(inventory)
You don't need to build a new inventory list every time you add a new item.
您无需在每次添加新项目时都构建新的库存清单。
回答by aIKid
That's not how to add an item to a string. This:
这不是向字符串添加项目的方法。这个:
newinv=inventory+str(add)
Means you're trying to concatenate a list and a string. To add an item to a list, use the list.append()
method.
意味着您正在尝试连接列表和字符串。要将项目添加到列表,请使用list.append()
方法。
inventory.append(add) #adds a new item to inventory
print(inventory) #prints the new inventory
Hope this helps!
希望这可以帮助!
回答by alpere
You can use:
您可以使用:
newinv=inventory+[add]
but using append is better since it doesn't create a new list:
但使用 append 更好,因为它不会创建新列表:
inventory.append(add)
回答by alpere
Let me fix your code
让我修复你的代码
inventory=["sword", "potion", "armour", "bow"]
print(inventory)
print("\ncommands: use (remove) and pickup (add)")
selection=input("choose a command [use/pickup]")
if selection == "use":
print(inventory)
inventory.remove(input("What do you want to use? "))
print(inventory)
elif selection == "pickup":
print(inventory)
add=input("What do you want to pickup? ")
newinv=inventory+[str(add)] #use '[str(add)]' or list(str(add))
print(newinv)
The error is you are adding string and list, you should use list
or []
to make the string become list
type
错误是您正在添加字符串和列表,您应该使用list
或[]
使字符串成为list
类型