I am working on a fantasy football project for my friend. The situation is that there are 16 players and each one has a different "weight" assigned to him based on their past performance in the league. What I have to do is make it so that a name is chosen randomly (but with a certain weight attributed to that name) and have that name be removed from the list.
I feel like this project should be relatively simple but I've been working on it for quite a while, and so far, no dice. What I'm getting stuck on is the fact that, after a name is removed from the list, the program still can sometimes select it on future runs. This is because I can't shrink my list while simultaneously adjusting the odds for the remaining players on the list. If the names are chosen at random, there are nearly endless permutations in which the odds would need to be reworked and adjusted when certain players are removed.
At first, I tried to remove items from the list based on index using the del function before realizing I couldn't do that; if I removed an item at index 13 and the list has shrunk to a length of 6, I would get an error. Seeing this was not possible, I instead tried using the remove method. I was faced again with the same problem; once a value is removed, it can still be chosen by the program leading to a value error.
To try and find a way around this, I implemented some try/except blocks in each clause, but, since some players are weighted highly and some are not, it is super likely that in each run, I'm stuck getting my error string "already chosen" many times before the list is empty.
As you can see, I've thought long and hard about the solutions to this problem but would now appreciate some help on how to progress forward. I appreciate any and all input. I apologize for the messy code, I'm very much so a beginner.
To understand where I got the numbers from, basically, I took the number associated with the first team, Bust A Kaep!, made the odds of choosing that 1 in 11.25 by making that the selected name if the number randomly chosen is in that range. I then added 10.27 to that number and made that the range for Truffle Shuffle to be chosen. These numbers add up to 100 and I followed this procedure. The rest of the numbers can be found in the dictionary towards the bottom of the code.
import random
def RNG(teams):
while len(teams)>0:
rando = round(random.uniform(1,101), 2)
if rando <= 11.25:
chosen = "Bust A Kaep!"
try:
print(chosen)
teams.remove("Bust A Kaep!")
print(teams)
except ValueError:
print('already chosen')
if rando > 11.25 and rando <= 21.52:
chosen = "Truffle Shuffle"
try:
print(chosen)
teams.remove("Truffle Shuffle")
print(teams)
except ValueError:
print('already chosen')
if rando > 21.52 and rando <= 29.76:
chosen = "Half-A$$"
try:
print(chosen)
teams.remove("Half-A$$")
print(teams)
except ValueError:
print('already chosen')
if rando > 29.76 and rando <= 37.38:
chosen = "Average Joe's Gym"
try:
print(chosen)
teams.remove("Average Joe's Gym")
print(teams)
except ValueError:
print('already chosen')
if rando > 37.38 and rando <= 45.00:
chosen = "The Abusement Park"
try:
print(chosen)
teams.remove("The Abusement Park")
print(teams)
except ValueError:
print('already chosen')
if rando > 45.00 and rando <= 52.41:
chosen = "Friends"
try:
print(chosen)
teams.remove("Friends")
print(teams)
except ValueError:
print('already chosen')
if rando > 52.41 and rando <= 59.68:
chosen = "Felis"
try:
print(chosen)
teams.remove("Felis")
print(teams)
except ValueError:
print('already chosen')
if rando > 59.68 and rando <= 66.79:
chosen = "Team of Steel"
try:
print(chosen)
teams.remove("Team of Steel")
print(teams)
except ValueError:
print('already chosen')
if rando > 66.79 and rando <= 72.59:
chosen = "Zach Butler"
try:
print(chosen)
teams.remove("Zach Butler")
print(teams)
except ValueError:
print('already chosen')
if rando > 72.59 and rando <= 78.24:
chosen = "SwagLords"
try:
print(chosen)
teams.remove("SwagLords")
print(teams)
except ValueError:
print('already chosen')
if rando > 78.24 and rando <= 83.68:
chosen = "HomeWreckers"
try:
print(chosen)
teams.remove("HomeWreckers")
print(teams)
except ValueError:
print('already chosen')
if rando > 83.68 and rando <= 88.04:
chosen = "Norman Island"
try:
print(chosen)
teams.remove("Norman Island")
print(teams)
except ValueError:
print('already chosen')
if rando > 88.04 and rando <= 91.91:
chosen = "Who Dey and the Blow Fish"
try:
print(chosen)
teams.remove("Who Dey and the Blow Fish")
print(teams)
except ValueError:
print('already chosen')
if rando > 91.91 and rando <= 94.78:
chosen = "Bleed Burgundy and Gold"
try:
print(chosen)
teams.remove("Bleed Burgundy and Gold")
print(teams)
except ValueError:
print('already chosen')
if rando > 94.78 and rando <= 97.60:
chosen = "Globo Gym Purple Cobras"
try:
print(chosen)
teams.remove("Globo Gym Purple Cobras")
print(teams)
except ValueError:
print('already chosen')
if rando > 97.60 and rando <= 100:
chosen = "The Todd Father"
try:
print(chosen)
teams.remove("The Todd Father")
print(teams)
except ValueError:
print('already chosen')
input("press enter to continue")
if len(teams) == 0:
print("ALL DONE!!")
int(input('press a number to continue'))
teams = list({"Bust A Kaep!" : 11.25, "Truffle Shuffle": 10.27, "Half-A$$":
8.24, "Average Joe's Gym": 7.62, \
"The Abusement Park": 7.62, "Friends": 7.41, "Felis": 7.27, "Team of Steel":
7.11, "Zach Butler": 5.80, "SwagLords": 5.65, \
"HomeWreckers": 5.44, "Norman Island": 4.36, "Who Dey and the Blow Fish":
3.87, "Bleed Burgundy and Gold": 2.87, \
"Globo Gym Purple Cobras": 2.82, "The Todd Father": 2.40})
RNG(teams)
Aucun commentaire:
Enregistrer un commentaire