When you do [ algo ]
, is creating a list (because of [ ]
), with a single element (algo
).
In your case, this algo
is nome * 3
, which is basically the string nome
"multiplied by 3". In Python, when you "multiply" a string by a number, the result is another string, with its contents repeated several times. That is, if nome
for a string "Samuel", then nome * 3
will be the string "Samuelsamuelsamuel".
Therefore, you are creating a list containing only one element: a string with the nome
repeated several times.
If the idea is to create a list with the same name several times, you could do [ nome ] * 3
(the list [ nome ]
contains the name once, and by "multiplying" the list by a number, you create a list with this element repeated several times), which results in a list with 3 elements (all equal to nome
).
Another point is that at the end you add this list into another list, but it doesn’t seem to be what you want.
What would have to be done is to have a single list, and you just insert it into it. Something like this:
nomes = []
for _ in range(3): # lê 3 nomes (pode mudar para quantos precisar)
# lê os dados e atualiza os nomes
valor = int(input('Digite o valor que você deseja doar: '))
nome = input('Qual o seu nome? ')
total, resto = divmod(valor, 10)
if resto == 0 and total > 0:
print(f'Você vai concorrer {total} vez{"es" if total > 1 else ""}.')
nomes.extend([ nome ] * total)
else:
print('valor inválido, deve ser múltiplo de 10 e maior que zero')
print(nomes)
Since the values should be multiples of 10, I used divmod
to take the result of the division by 10 and the rest of this division. Thus, if the rest is not zero, the value is invalid.
Then I use [ nome ] * total
to create the list with the repeated name several times, and use extend
to add these elements to the list nomes
.
I put in a loop to read several names, but then you can adapt accordingly.
Then you can shuffle with random.shuffle
, if you want:
from random import shuffle
shuffle(nomes)
Although, to draw a random amount, just use random.choice
(maybe you don’t even have to shuffle, since choice
will choose an item from the list randomly, and whatever position they are in):
from random import choice
sorteado = choice(nomes)
Hello guys, sorry, I already made the correction.. First time using here, I’m still getting acquainted. But I’ve already edited and put as requested!
– Samuel Teixeira
@hkotsubo I had not committed myself on this, I can use the Random.shuffle in the same names list, right?
– Samuel Teixeira
I want to do the following, let’s assume that Samuel donated 40 real. Samuel’s name has to appear 4 times on the list. If Bia donated 20, her name has to appear 2 times. I need to list some "donors" with their respective donations. To draw lots with these people.
– Samuel Teixeira
Considering that the name of the donor will appear 1 time for every 10 reais you donate. Da para entender? I don’t know if I expressed myself well
– Samuel Teixeira
Then re-edit the question and put it all there. In the current code it is not clear that it was to put multiple names.
– hkotsubo
@hkotsubo went badly :( edited again.
– Samuel Teixeira
Ah, what if the value is 25? 32? 49? Will round? Or do you only accept multiple values of 10 (like 10, 20, 30, etc)?
– hkotsubo
@hkotsubo Values will be (10, 20, 30).
– Samuel Teixeira