0

Im pretty unexperienced and this is my first language so any pointers and tips will be gladly welcomed.

Im trying to create a raffle program where it asks how many participants are and with that maximum number draw out one winner. I tried with:

import random
participants = int(input('How many participants?'))
list = [participants]
winner = random.choice(list)
print('Congratz {}, you are the winner!!!'.format(winner))


  

antonimooo
  • 19
  • 2
  • 1
    You are creating the list of numbers wrong. Have you tried printing out your list? See [How can I generate a list of consecutive numbers?](https://stackoverflow.com/q/29558007/2745495) – Gino Mempin Apr 21 '21 at 21:48
  • not required at all in this case as they indeed are consecutive numbers. – DevLounge Apr 21 '21 at 21:51
  • @DevLounge It's not required, but that's the problem with the OP's code, that there is no actual list. – Gino Mempin Apr 22 '21 at 00:04

2 Answers2

2

You do not need to create a list and then pick one of them. Just use a random integer between 1 and number of participants.

import random

participants = int(input('How many participants? :'))
winner = random.randint(1, participants + 1)
print(f'Congratz {winner}, you are the winner!!!')
DevLounge
  • 8,313
  • 3
  • 31
  • 44
-1

small modification of the code should do it:

import random
n_participants = int(input('How many participants?'))
winner = 1 + random.choice(range(n_participants))
print('Congratz {}, you are the winner!!!'.format(winner))
anon01
  • 10,618
  • 8
  • 35
  • 58