Add Entries to a Multidimensional List using input in Python

Asked

Viewed 232 times

0

there Python master, I have the following code:

veiculos = [['Fusca', 'Escort', 'BMW'],['1600W', 'XR3', '325i']]
print(veiculos)
print(veiculos[0][0],veiculos[1][0])
print(veiculos[0][1],veiculos[1][1])
print(veiculos[0][2],veiculos[1][2])

for i in range(len(veiculos)):
    for j in range(len(veiculos[i])):
        print(veiculos[i][j], end=' - ')
    print()

I would like to know how to make the user enter this data, ie using input, the user type the vehicle name, then type the vehicle model, using multidimensional list. I am waiting for a help, who is seeking to learn, very grateful.

  • And it needs to be on two separate lists?

  • I would suggest you take a step back, and study "dictionaries" in Python - even if you don’t use dictionaries, the best thing there is to group the data that goes together, using "list lists" instead of having the data diferents of the same entry in separate lists -

1 answer

0

I believe that you need to use a repetition loop to request the data through input and the append function to add the items in the list, would be more or less in the example below

Note: you will have to organize the prints whatever you did

user = ''
user2 = ''
stop = 'S'
veiculos = list()

while stop == 'S':
    user = str(input("Nome do carro:"))
    user2 = str(input("Modelo do carro:"))
    stop = str(input("Deseja continua? [S/N]: ")).upper()
    veiculos.append([user])
    veiculos.append([user2])


print(veiculos)


for i in range(len(veiculos)):
    for j in range(len(veiculos[i])):
        print(veiculos[i][j], end=' - ')
    print()

Browser other questions tagged

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