Can only concatenate list to list 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 
    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.

asked Oct 16, 2013 at 8:54

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.

answered Oct 16, 2013 at 8:56

jabaldonedojabaldonedo

25.1k8 gold badges75 silver badges76 bronze badges

0

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.

inventory.append[add] #adds a new item to inventory
print[inventory] #prints the new inventory

Hope this helps!

answered Oct 16, 2013 at 8:58

aIKidaIKid

24.8k4 gold badges38 silver badges65 bronze badges

3

You can use:

newinv=inventory+[add]

but using append is better since it doesn't create a new list:

inventory.append[add]

answered Oct 30, 2019 at 11:15

alperealpere

1,02617 silver badges24 bronze badges

I have a solution for this. First thing that add is already having a string value as input[] function by default takes the input as string. Second thing that you can use append method to append value of add variable in your list.

Please do check my code I have done some modification : - {1} You can enter command in capital or small or mix {2} If user entered wrong command then your program will ask to input command again

inventory = ["sword","potion","armour","bow"] print[inventory] print["\ncommands : use [remove item] and pickup [add item]"] selection=input["choose a command [use/pickup] : "] while True: if selection.lower[]=="use": print[inventory] remove_item=input["What do you want to use? "] inventory.remove[remove_item] print[inventory] break

 elif selection.lower[]=="pickup":
      print[inventory]
      add_item=input["What do you want to pickup? "]
      inventory.append[add_item]
      print[inventory]
      break
 
 else:
      print["Invalid Command. Please check your input"]
      selection=input["Once again choose a command [use/pickup] : "]

answered Nov 1, 2020 at 16:29

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

answered Oct 30, 2019 at 11:19

Can only concatenate list not tuple to list?

The Python "TypeError: can only concatenate tuple [not "list"] to tuple" occurs when we try to concatenate a tuple and a list. To solve the error, make sure the two values are of type list or type tuple before concatenating them.

Can you concatenate a list 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.

Can you only concatenate a string 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.

Can only concatenate tuple not int to tuple meaning?

The Python "TypeError: can only concatenate tuple [not "int"] to tuple" occurs when we try to concatenate a tuple and an integer. To solve the error, make sure you haven't got any dangling commas if trying to sum two integers, otherwise declare 2 tuples.

Chủ Đề