0

I am working on a program that will choose a winner of multiple users and display the winner. I have calculated the percentage number which will be the amount of times the username will be appended to a list.

To clarify:

  1. Calculate the percentage
  2. Append the users username to a list the same amount of times as the percentage number.
  3. Choose a random person from the list.
  4. Display the winner.

Heres my code so far:

probabilities = []

    print("Calculating", player1 + "'s luck!")
    time.sleep(3)
    player1num = random.randint(0,100)
    newnum = player1num / 2
    newnumadded = newnum + 10
    if newnumadded > 50:
        bonusnum = newnumadded + 25
        print(player1 + " Your number is:", str(player1num) + "! You have a percentage to win of: ", str(bonusnum) + "%!")
    else:
        print(player1 + " Your number is:", str(player1num) + "! You have a percentage to win of: ", str(newnumadded) + "%!")

I am trying to make it so that the player1 variable (which is the users name) will be stored inside of the probabilities list the same number of times as the percentage number.

Example: John, has a percentage chance to win of 79%. Add John to the list 79 times

Matt Jones
  • 13
  • 1
  • 7
  • or this: http://stackoverflow.com/questions/9259989/select-random-item-with-weight or even maybe: http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice – Lafexlos Feb 04 '16 at 21:43

2 Answers2

1

If you want to add player1 to the list percentage_list a total of percentage_number times you can do:

percentage_list.extend([player1] * percentage_number)

cowlicks
  • 1,037
  • 1
  • 12
  • 20
0

Just append x element for x times in your list:

import random

probabilities = []
player1="John"

print("Calculating", player1 + "'s luck!")

player1num = random.randint(0,100)
newnum = player1num / 2
newnumadded = newnum + 10
if newnumadded > 50:
    bonusnum = newnumadded + 25
    print(player1 + " Your number is:", str(player1num) + "! You have a percentage to win of: ", str(bonusnum) + "%!")
    probabilities.extend([player1 for x in range(0,int(bonusnum))])

else:
    print(player1 + " Your number is:", str(player1num) + "! You have a percentage to win of: ", str(newnumadded) + "%!")
    probabilities.extend([player1 for x in range(0,int(newnumadded))])

print(probabilities)
Gavelock
  • 77
  • 8
DarkFranX
  • 361
  • 2
  • 13