Frank Alves
Frank Alves

Reputation: 3

Add more values in a list at once in Python

I'm a beginner in Python and I'm trying to solve this problem. I'm trying to write a code where you can put your name and the amount that you want to donate. The thing is, deppending on the amount of the donation you can have more chances to be the winner. Eg. If you donate $10 (1 chance), $20(2 chances), $30(3 chances). My biggest problem is because I can't figure out how to solve this problem when the person insert $30 its name goes to the list 3 times and so on. I tried to use "for..inrange():" but without any sucess. Can someone explain me how to do this?

from random import shuffle
from random import choice

list = []

while True:
    name = str(input('Write your name: '))
    donation = float(input('Enter the amount you want to donate.: $ '))
    list.append(name)
    print('You donated $ {}. Thank you {} for you donation!'.format(donation, name))

    print('=-'*25)
    print('[1] YES')
    print('[2] NO')
    answer = int(input('Would you like to make another donation? '))
    if answer == 1:
        continue
    else:
        shuffle(list)
        winner = choice(list)
        break

print('The winner was: {}' .format(winner))

Upvotes: 0

Views: 138

Answers (5)

Samwise
Samwise

Reputation: 71542

Rather than adding extra names to the list to represent the higher chance, you could use the donations as weights in the random.choices function:

from random import choices

names, donations = [], []

while True:
    names.append(input('Write your name: '))
    donations.append(float(input('Enter the amount you want to donate.: $')))
    print(f'You donated ${donations[-1]}. Thank you {names[-1]} for your donation!')
    print('=-'*25)
    print('[1] YES')
    print('[2] NO')
    if input('Would you like to make another donation? ') != '1':
        break

winner = choices(names, donations)[0]
print(f'The winner was: {winner}')

This allows for non-integer donations to be counted fairly -- e.g. if Bob donates $0.25 and Fred donates $0.50, the drawing will still work in a reasonable way. It also allows very large donations to be handled without tanking the performance of the program -- if you have one list entry per dollar donated, what happens if Elon donates $20B and Jeff donates $30B? (The answer is that your fan spins really fast for a while and then the program crashes because you can't create a list with 50 billion elements -- but this is not a problem if you simply have a list of two elements with large int values.)

Note that shuffle is not necessary if you're using random.choices (or random.choice for that matter) because those functions will already make a random selection from the list.

Upvotes: 1

Abhinav Singhal
Abhinav Singhal

Reputation: 182

This code should do the job. Please follow good naming conventions as pointed out by others. I have changed the list variable to donations as it is forbidden to use keywords as variables.

I have included the name in donations int(name) // 10 times using the extend function as pointed out by others. You may change the number of times as you wish.

from random import shuffle
from random import choice

donations = []
makeDonation = True
winner = "Unknown"
while makeDonation:
    name = str(input('Write your name: '))
    donation = float(input('Enter the amount you want to donate.: $ '))
    donations.extend([name for i in range ( int(donation) // 10)])
    print('You donated $ {}. Thank you {} for you donation!'.format(donation, name))
    print('=-'*25)
    print('[1] YES')
    print('[2] NO')
    answer = int(input('Would you like to make another donation? '))
    if answer == 2:
        makeDonation = False

shuffle(donations)
winner = choice(donations)
print('The winner was: {}' .format(winner))

Upvotes: 0

Kache
Kache

Reputation: 16737

An alternative to adding another loop with additional control flow, you can use list.extend() with a list expression:

num_chances = donation // 10
chances = [name] * num_chances
all_chances.extend(chances)

Note that list is a built-in python identifier, and it's not a good idea to overwrite it. I've used all_chances instead.

Upvotes: 1

Prune
Prune

Reputation: 77890

First do not use the name of a built-in type as a (meaningless) variable name. Change list to entry_list.

For the particular problem

  • compute the quantity of chances;
  • make a list of the person's name that many times;
  • extend the entry list with that list of repeated name.

Code:

entry_list = []

while ...
    ...
    chances = int(donation) // 10
    entry_list.extend( [name] * chances )

Upvotes: 1

zoldxk
zoldxk

Reputation: 1

You can use a for loop to append the name to the list more than one time :

for i in range(donation//10):
    list.append(name)

Upvotes: 0

Related Questions