How to distribute inputs in lists randomly?

Asked

Viewed 77 times

0

How do I input nome + numero + outro in one thing and then place them in one of those lists (A, B, C ,D) randomly in the Python language?

A = []
B = []
C = []
D = []

for k in range(12):
    nome = input('Digite seu nome: ')
    numero = int(input('Um numero: ')
    outro = int(input('Outro numero: ')

1 answer

1


You can use the function choice module random passing a list with all its lists as argument. What this function does is to return an element from that list randomly.

lista_de_nomes = ["João", "Maria", "Pedro", "Lucas", "Gabriela", "Paulo", "Roberta"]
nome_aleatorio = random.choice(lista_de_nomes) # Retorna um nome aleatório.

Therefore, you just need to pass all your lists to this function and then add to the returned list the data entered by the user. See the code below:

import random

A, B, C, D = [], [], [], []

for k in range(12):

    nome = input('Digite seu nome: ')
    numero = int(input('Um numero: '))
    outro = int(input('Outro numero: '))

    lista = random.choice([A, B, C, D])
    lista.append(nome + " " + str(numero) +" " + str(outro))
  • Friend, how do I make these lists to be limited to 3 sets of name, number and other?

  • As this is another question, create a new question on the site about.

Browser other questions tagged

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