How to add a value key to a dictionary?

Asked

Viewed 71 times

1

I want to add terms to an organized dictionary-like glossary, requested by an activity, but I’m getting an error message that I can’t identify why.

   glossario = {
    'concatenar': 'Concatenar é a junção de 2 cadeias de caracteres'
        ' e que dá origem a uma nova string' +
        ' que é formada pela junça das 2 partes.',
        'identar': 'Indentar é o recuo do texto em relação a sua margem',
    'array': 'Arrays são estruturas de dados' +
            ' semelhantes às listas do Python',
    'string': 'String é um objeto iterável.',
    }
for palavra, significado in set(glossario.items()):
    print(palavra.title() + ":" + "\n" + significado + '\n')
    
glossario['sequência'] = 'Sequências são coleções ordenadas embutidas:' + 
    'strings, listas, tuplas e buffers.'

and receive the following error message:

glossario['sequência'] = 'Sequências são coleções ordenadas embutidas:' +
                                                            ^
SyntaxError: invalid syntax
  • 3

    And what do you need to make this meaningless concatenation for?

  • to respect PEP 8 and not exceed 79 characters per line

  • 2

    And it wouldn’t be better to respect common sense?

  • glossario['sequence'] = 'Sequences are built-in ordered collections:' +str('strings, lists, tuples and buffers.') this can help you?

  • @The problem is that the statement continues in the next line - see the answer below - and 'strings, listas, tuplas e buffers' is already a string, use str around doesn’t change anything - it’s actually redundant...

1 answer

6


You should not force a concatenation just to break the line, you should indicate that the line continues just below, for that use the \ to indicate that it is the same line even having a break:

glossario['sequência'] = 'Sequências são coleções ordenadas embutidas:' \
    'strings, listas, tuplas e buffers.'

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • thank you very much! did not know this resource

Browser other questions tagged

You are not signed in. Login or sign up in order to post.