Database in Tupla

Asked

Viewed 245 times

1

I need a code that receives data (values) and prints them according to your key. Each entry corresponds to a given of a hypothetical person, which contains Age, Name, Sex, Marital Status, Number of friends and Number of Photos. I am writing using a dictionary that receives the entries as values and relates them to the keys. How do I do this using Tuples and so that the data is printed in the order it was provided by the user? How am I doing:

Dados = dict()
entrada1 = raw_input()
entrada2 = raw_input()
entrada3 = raw_input()
entrada4 = raw_input()
entrada5 = raw_input()
entrada6 = raw_input()
Dados['Idade'] = entrada1
Dados['Nome'] = entrada2
Dados['Sexo'] = entrada3
Dados['Estado'] = entrada4
Dados['Amigos'] = entrada5
Dados['Fotos'] = entrada6
for chave, valor in Dados.items():
    print chave, valor

Also, the code is not printing the keys and values so that a data appears per line, but printing the entire dictionary.

1 answer

1


The simplest way is to use the OrderedDict instead of the dict inlaid - OrderedDict is a data structure similar to a dictionary, available in the module collections standard library - but unlike a normal dictionary, preserves the order in which the key and value pairs were created when interacting on the same.

This way, all you need to do in your program is import Ordereddict and declare Dados as one of them - the rest you can leave as is:

from collections import OrderedDict
Dados = OrderedDict()
...
  • My problem is that the user is the one who determines the order of the data. If he wants to start with Name, then Age, then Friends, anyway. I need to write the code in a way every data provided is "automatically" related to its type. If you enter a name, your key should be Name. If you enter M, your key should be Gender. S - Marital status (single), and so on. I cannot determine that the first input is used as Name, for example. This is my lock.

Browser other questions tagged

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