Creating a multi-value dictionary for a key

0

I'm trying to create a code to register with input , as exercise.

dic = {}
    ...
def cadastrar():
   nome = input('Nome: ')
   variavel1 = input('variavel 1,: ')
   variavel2 = input('Variavel 2: ')
   dic.update({'nome': 'variavel1', 'variavel2'})

while True:
    opcao = exibirMenu()
    if opcao == ('cadastrar'):
        cadastrar()

This syntax error code, I change the code to:

dic.update('nome', 'varriavel1', 'variavel2')

and returns the error

  

TypeError: update expected at most 1 arguments, got 2

How do I save the data together, nome = variavel1, variavel2 . so you can access the name and return the variables appended to the name?

    
asked by anonymous 07.07.2018 / 17:51

1 answer

1

You can register a tuple. To create a tuple, the two variables must be enclosed in parentheses:

dic.update({'nome': (variavel1, variavel2)})

It is also important to remember that when a variable is referenced, no quotation marks are used. 'variavel1' is a string constant whose value is 'variavel1' , not the value that actually is in the 'variavel1' variable.

To access the tuple values in your dictionary, you would do this:

print(dic['nome'][0])  # variavel1
print(dic['nome'][1])  # variavel2
    
07.07.2018 / 19:31