How to show the user lenght of a list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to show the user lenght of a list?

I was trying to code this "random result generator" to practice a little with lists and while but I couldn't find a way to make the number of items in the list to be shown to the user before generating the result. What is wrong in this case? # -*- coding utf-8 -*- # #Na versão 1.0a o sorteio foi implementado com sucesso #o tamanho da lista de itens não foi gerado, porém. ##GERADOR DE RESULTADOS ALEATÓRIOS## print("--Sorteio Automático v1.0a--") print("----------------------------") print("Insira a seguir os nomes ou \nvalores a serem sorteados.") sair = False adicionar = True lista_sorteio = [] tamanho = len(lista_sorteio) while adicionar == True: entrada = input("Insira um item: ") lista_sorteio.append(entrada) print(tamanho, "itens adicionados à lista.") teste = input("Adicionar novo item? (s/n): ") if teste == "n": adicionar = False print("----------------------------") print("Vamos ao sorteio.") print("----------------------------") input("Aperte enter para sortear.") import random resultado = random.choice(lista_sorteio) print("O sorteado foi: ") print(resultado) input("Obrigado por utilizar Sorteio Automático v1.0a")

8th Jul 2020, 12:01 PM
Gabriel Luppi
Gabriel Luppi - avatar
1 Answer
+ 1
Your are taking length of list before loop, and not changing or calculating after input... Is that what you looking..?
8th Jul 2020, 12:25 PM
Jayakrishna 🇮🇳