0

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 :)

  • 1
    Maybe Google about for other examples, see what they do, expand yours similar to them or in better/more interesting way. Examples: https://gist.github.com/codeboy101/81e417f94299fe803e4a, https://github.com/topics/football-manager?q=python – MDR Aug 06 '21 at 19:04
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read [ask]. SO is meant for specific problems, so people here are going to focus on your main question and not the other problems with your code. You could ask on [codereview.se] if you'd like. – wjandrea Aug 06 '21 at 19:09
  • 1
    That said, you might consider using a dict to store the players instead of separate variables like `goal_keeper` etc. See [How do I create variable variables?](https://stackoverflow.com/q/1373164/4518341) Then you could simplify all the `if position_input == ...` down to `random.choices(players[position_input], k=n)` – wjandrea Aug 06 '21 at 19:14

1 Answers1

1

In the end, if you have a list with hundreds of players you will need to define them somewhere. To keep the code clean I would create a file for each position and then have one player name per line. Then you can do the following:

with open('defender.txt','r') as f:
    defender = f.readlines()

Like this you can have a file with hundreds of players without messing up your code.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
user_na
  • 2,154
  • 1
  • 16
  • 36