如何将项目添加到列表中的字典?

时间:2020-05-04 15:16:53

标签: python dictionary input data-structures append

你好,我正在开发一款基于文本的游戏,我在为drop命令苦苦挣扎;它的工作方式是您输入“ D 项目名称”,然后检查该项目是否确实在库存中,如果存在,则将其放入变量,将其从库存中删除,我希望将其附加到房间(字典)的内容索引上,并且该字典在列表中,但是我不能附加到它。 这是代码(部分代码):

room = []
room.append({'number': 1, 'content': ""})
roomnumber = 1
inv = ["sword"]
command = input(": ")
first_letter = command(0)

if first_letter == "D":
   item = command.split(" ", 2)
   item.remove("D")
   for i in range(0, len(inv):
      inv.pop(i)
#this doesn't work
`     room[roomnumber]['content'].append(item[0])`
      item.pop(0)

我输入:“ D剑”后,出现此错误:

Traceback (most recent call last):
File "/Users/antony/PycharmProjects/TextBased/Main.py", line 54, in <module>
room[roomnumber]['content'].append(item[0])
AttributeError: 'str' object has no attribute 'append'

我不明白,请帮助!

3 个答案:

答案 0 :(得分:0)

您是否要让一个房间容纳多个物品?如果是这样,请将您的content字段设为列表而不是字符串:

room.append({'number': 1, 'content': []})

现在您可以append content任意数量的东西。

答案 1 :(得分:0)

room = []
room.append({'number': 1, 'content': ""})
## room = [{'number': 1, 'content': ""}]

roomnumber = 1
## you should actually change this to 0. Otherwise you will get an "index out
## of range" error (see comment below)

inv = ["sword"]
command = input(": ")
first_letter = command(0)

if first_letter == "D":
   item = command.split(" ", 2)
## why are you adding a max split here??

   item.remove("D")
   for i in range(0, len(inv)):
## equals for i in range(0, 5):, so iterates 0,1,2,3,4
## you forgot to add a closing bracket for the range

      inv.pop(i)
## what are you trying to do here? This looks strange to me, given that after each
## pop, the value of your inv will be shortened..?

#this doesn't work
      room[roomnumber]['content'].append(item[0])
## this does not work because your room list only contains one element, a 
## dictionary, at index position 0. Probably THIS is your biggest issue here. 
## Second, you're trying to change the value of 'content'. To change the value of a
## dictionarie's key, you need to use "=", and not ".append", as ".append" is used
## to append an element at the end of a list, to a list, and not to a dictionary. 
## So, use room[roomnumber]['content'] = item[0] 
      item.pop(0)

据我了解,您想根据房间号将内容添加到相应房间号的字典的内容值中。在这种情况下,您的整个语法是错误的,您必须使用:

room = []
room.append({1:""})

## and then, after the rest of the code:

room[roomnumber-1][roomnumber] = item[0]

或更简单,因为实际上列表和字典的同时使用已经过时了

## initiate one dictionary, containing all the rooms in key = roomnumber
## value = content pairs
rooms = {}

## the syntax to add a new room number with a new content into the dictionary
## would then simply be: rooms[number] = content, e.g.:
rooms[1] = ""

## to then set the value of the content for a given roomnumber, you simply use
rooms[roomnumber] = item[0]

我建议您了解python中列表和字典之间的基本区别,您似乎对如何访问/修改列表和字典的元素缺乏基本的了解(当然没有冒犯)

答案 2 :(得分:0)

好的,非常感谢大家:),我只是想说有些事情有点怪异的原因是因为那只占代码的20%(例如更多的房间,例如女巫,这就是为什么我需要在列表中使用字典),而我的房间号在我的实际代码中确实以0开头:),弹出窗口是从清单中删除该项目(因为这是drop命令),而我从item变量中删除它只是为了确保不会导致任何有害错误。否则,是的,该内容原本应该是一个列表,我忘了,感谢您指出它,并且for循环实际上已将其关闭(编写此文件时,我有点着急)。无论如何,谢谢大家:)