vendredi 6 août 2021

Better way of storing more elements and then randomly selecting specific quantities of them

Hi I'm extremely new to python and was just wondering if anyone could help me/point me in the right direction, to improve my code.

import random

goal_keeper = ["Neuer","Allison", "De Gea"]
defender = ["Varane", "Ramos", "Thiago Silva","Chiellini", "Shaw", "Alaba"]
midfielder = ["De Bruyne", "Frenandes", "Modric", "Thiago","Sane", "Rashford"]
attacker = ["Lewandowski","Messi","Ronaldo","Mbappe","Neymar","Kane"]

i = int(input("How many players do you want?: "))
n_plyr = 0
squad = []
while n_plyr < i:
    position_input = input("Select the players position: ")
    n = int(input("How many players in that position?: "))
    n_plyr = n_plyr + n
    print(i-n_plyr, "left to pick for your squad")

    if position_input == "goal keeper":
        gk_players = random.choices(goal_keeper, k=n)
        squad.append(gk_players)
    if position_input == "defender":
        def_players = random.choices(defender, k=n)
        squad.append(def_players)
    if position_input == "midfielder":
        mid_players = random.choices(midfielder, k=n)
        squad.append(mid_players)
    if position_input == "attacker":
        atk_players = random.choices(attacker, k=n)
        squad.append(atk_players)
    if n_plyr > i:
        n_plyr = n_plyr - n
        print("Squad is too big only", i-n_plyr, "left to pick")

print(routine)

The code does work im not sure how to adapt it to adding more players as expanding the list doesn't seem the right thing to do especially if I wanna add hundreds of each position. what is the best way at storing a lot more elements and then still being able to select a random number of them like i tried to above?. Also any other suggestions will be appreciated, just trying to learn and improve :)




Aucun commentaire:

Enregistrer un commentaire