Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: can only concatenate list (not "str") to list

Tags:

python

I am trying to make an inventory program to use in an RPG. The program needs to be able to add and remove things and then add them to a list. This is what I have so far:

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 something 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, it would be greatly appreciated.

like image 200
user2885647 Avatar asked Oct 16 '13 08:10

user2885647


People also ask

Can only concatenate list not method to list?

The “TypeError: can only concatenate list (not “int”) to list” error is raised when you try to concatenate an integer to a list. This error is raised because only lists can be concatenated to lists. To solve this error, use the append() method to add an item to a list.

Can only concatenate str not list to string in Python?

The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list. Here is an example of how the error occurs. Copied!

Can only concatenate str error in Python?

The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer. To solve the error, convert the int to a string, e.g. str(my_int) to concatenate the strings or convert the str to an int, e.g. int(my_str) to add the numbers.

How do you concatenate lists in Python?

Python's extend() method can be used to concatenate two lists in Python. The extend() function does iterate over the passed parameter and adds the item to the list thus, extending the list in a linear fashion. All the elements of the list2 get appended to list1 and thus the list1 gets updated and results as output.


1 Answers

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.append(add) 

What you are doing now is trying to concatenate a list with a string which is an invalid operation in 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 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.

like image 120
jabaldonedo Avatar answered Sep 19 '22 09:09

jabaldonedo