0

I'm an absolute amateur when it comes to any coding but I have been attempting to create a top trumps type program using Python. In the below code I have created player cards that are added to a list using a class. I am now attempting to randomly split the list (fullCardList) into two separate lists but having a lot of issues. Once the list has been split, I am aiming to compare a stat from each player but cannot seem to figure how to show the card's name/caps/goals. I hope this makes sense. Can anyone give me any advice?

class Player:
def __init__(self, name, caps, goals, trophies):
    self.name = name
    self.caps = caps
    self.goals = goals
    self.trophies = trophies


CardListOne = Player("Lionel Messi",102, 46, 26)
CardListTwo = Player("Ronaldo", 124, 55, 17)
CardListThree = Player("Mats Hummels", 39, 4, 8)
CardListFour = Player("Angel Di Maria", 65, 15, 10)
CardListFive = Player("Jason", 101,44,12)
CardsListSix = Player("Peter", 45,10,1)

fullCardList = [CardListOne.name, CardListOne.caps,CardListOne.goals,CardListOne.trophies],[CardListTwo.name,CardListTwo.caps,CardListTwo.goals,CardListTwo.trophies],[CardListThree.name, CardListThree.caps, CardListThree.goals, CardListThree.trophies], [CardListFour.name,CardListFour.caps,CardListFour.goals,CardListFour.trophies], [CardListFive.name,CardListFive.caps,CardListFive.goals,CardListFive.trophies]

global playersRandomSelection, computersRandomSelection
playersRandomSelection = []
playersRandomSelection = random.sample(fullCardList,3)

computersRandomSelection = []
computersRandomSelection = random.sample(fullCardList,3)


print("Players name selection: " + str(playersRandomSelection))
print("Computers random selection: " + str(computersRandomSelection))
Haddo
  • 1
  • Are you looking for something like this: https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print? – ForceBru Jun 23 '20 at 19:40
  • What would your expected output look like? – Juan C Jun 23 '20 at 19:41
  • What's the question? The code you posted should work. What have you tried with regard to splitting it, etc.? – BruceWayne Jun 23 '20 at 19:43
  • @BruceWayne the plan is to randomly split fullCardList into two separate lists. Then my print statement should say "Players name selection: Lionel Messi". Maybe further down the code, i can add "Lionel Messi has: 102 caps". Sorry if i wasnt very clear. – Haddo Jun 23 '20 at 19:55

1 Answers1

1

Hhow to show the card's name/caps/goals/(trophies):

You can use the method, vars():

lst = [CardListOne,CardListTwo,CardListThree,CardListFour,CardListFive,CardsListSix]
for n in lst:
    a = vars(n)
    print('\n'.join(f"{k}: {v}" for k,v in a.items()))
    print()

Output:

name: Lionel Messi
caps: 102
goals: 46
trophies: 26

name: Ronaldo
caps: 124
goals: 55
trophies: 17

name: Mats Hummels
caps: 39
goals: 4
trophies: 8

name: Angel Di Maria
caps: 65
goals: 15
trophies: 10

name: Jason
caps: 101
goals: 44
trophies: 12

name: Peter
caps: 45
goals: 10
trophies: 1
Red
  • 26,798
  • 7
  • 36
  • 58