0
itens = []
lugares = []
lugar = None
from pickle import dumps
# Definição das funções:
def adlugar(nome, lookmsg, passagens, objs = None):
global lugares
global dic
dic = dumps(nome)
dic = {"nome": nome, "look": lookmsg, "passagens": passagens.split(" # ")}
if objs:
itens = objs.split(" # ")
dic["invent"] = itens
lugares.append(nome.lower())
def aditen(nome, lookmsg, getmsg, pegavel = "não"):
global itens
global dic
dic = dumps(nome)
dic = {"nome": nome, "look": lookmsg, "get": getmsg, "pg": pegavel}
itens.append(nome.lower())
def look(obj):
global itens
global lugar
if obj in itens:
obj = obj.lower()
obj = dumps(obj)
if obj in lugar["invent"]:
return(obj[look])
lugar = dumps("casa")
adlugar("casa", "casa.", "quintal", "livro")
aditen("livro", "livro", "livro", "sim")
look("livro")
I have problems handling dictionaries. I have defined a function that creates dictionaries, and another that invokes values from indices. But within the function that calls the values I get the following error:
if obj in lugar["invent"]: TipeError: byte indices must be integers or slices, not str
First, when I use the adlugar() function, it should create a dictionary, in this case: home = {"name": "house", "look": "house", "passages": "yard", "Invent": "book"} The aditen function should create a book dictionary = {"name": "book", "look": "book", "get": "book", "pg": "yes"}. Note that the two functions use the name attribute to create a dumps(name) variable for the dictionary (the reason is that you cannot create string variables, and my solution was to use dumps to transform them into byte sequences). Already the look function uses the variable place to know in which dictionary it should check the index "Invent". If the object is in this index, it returns what is in the "look" index of the obj. However, when I invoke the command, it says that my index cannot be a string, but rather an "integer" or an "Slice", which are list indexes. That means he’s treating the dictionaries as if they were lists?
Your code, in general, doesn’t make much sense. Is there any way you can describe your problem and comment on what each part of your code should do? Notes: 1) Using the function
dump
before creating the object makes no sense; 2) Doreturn(print(obj[look]))
also makes no sense; 3)lugar
is a string, access the indexinvent
it doesn’t make sense (this is the cause of the current error); 4) What should this beinvent
which is not used elsewhere in the code?– Woss
Pedro, just below your question, there is the link edit, use it and add the content of these two comments to the question, as they are essential for someone to seek to understand their problem. If possible, also say why you are using the library
pickle
.– Woss
Slice returns a part of the list in question. For example, make the list
foo = [0, 1, 2, 3]
and putprint(foo[0:2])
, will return[0, 1]
, because it returns the part of the listfoo
starting at position 0 with 2 elements. The expression0:2
, in this case, is what is called Slice.– Woss