0

I am writing a game which has a set of rules to determine which card is a top trump however, when i want to say if "R" for example is in the word R1 do something, as i think that is my error, much appreciated help, Thanks. My game has 3 different colours Black Red and yellow which has a hierachy system for example Black Beats yellow but red beats Black and yellow beats red, this hierachy system is used always unless both colours are the same which then uses the higher number which i have not implemented yet.

import random
authorisedPlayers=["Jeffrey","Collin", "Collina","Danny","JephTheDuck","JerryJaye"]
cards = [["R1","R2","R3","R4","R5","R6","R7","R8","R9","R10"],["Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8","Y9","Y10"],["B1","B2","B3","B4","B5","B6","B7","B8","B9","B10"]]
PlayerOneDeck=[]
PlayerTwoDeck=[]
Player1=input("Enter Player 1: ")
Player2=input("Enter Player 2: ")
if (Player1 and Player2) in authorisedPlayers:
  print("Hello " ,Player1,"and",Player2)
  for i in range(0,14):
    player1Hand=random.choice (cards)
    cards.remove(player1Hand) 
    player2Hand=random.choice (cards)
    cards.remove(player2Hand) 
    if "R" in player1Hand and "B" in player2Hand:
      PlayerOneDeck.append(player1Hand)
      PlayerOneDeck.append(player2Hand)
    elif "R" in player2Hand and "B" in player1Hand:
      PlayerTwoDeck.append(player1Hand)
      PlayerTwoDeck.append(player2Hand)

    
    elif "Y" in player1Hand and "R" in player2Hand:
      PlayerOneDeck.append(player1Hand)
      PlayerOneDeck.append(player2Hand)
    elif "Y" in player2Hand and "R" in player1Hand:
      PlayerTwoDeck.append(player1Hand)
      PlayerTwoDeck.append(player2Hand)

    elif "B" in player1Hand and "Y" in player2Hand:
      PlayerOneDeck.append(player1Hand)
      PlayerOneDeck.append(player2Hand)
    elif "B" in player2Hand and "Y" in player1Hand:
      PlayerTwoDeck.append(player1Hand)
      PlayerTwoDeck.append(player2Hand)
  
  lenP1=len(PlayerOneDeck)
  lenP2=len(PlayerTwoDeck) 
  if lenP1>lenP2:
    print(Player1," wins with ",lenP1,"cards!!!")
  else:
    print(Player2, " wins with ",lenP2,"cards!!!")
  #NOW MAKE SURE YOU SAY IF B IS IN THE STRING OR A IS WHICHEVER ONE HAS B IS BETTER ETC AND ITERATE 15 TIMES
else:
  print("Sorry Not an Authorised User")

My error message is :

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    player2Hand=random.choice (cards)
  File "/usr/lib/python3.8/random.py", line 290, in choice
    raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence

Please Help, it is much appreciated, Sorry if my grammar and format is not to your acceptable standard .

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You have other problems. `if (Player1 and Player2) in authorisedPlayers:` is wrong, it should be `if Player1 in authorisedPlayers and Player2 in authorisedPlayers:` – Barmar May 18 '22 at 20:54
  • `Player1Hand` is a list like `["R1","R2","R3","R4","R5","R6","R7","R8","R9","R10"]`. There is no `R` in this list. It contains `R1`, `R2`, etc. but not `R` by itself. You want `if any('R' in card for card in Player1Hand)` – Barmar May 18 '22 at 20:56
  • how would i do that barmar? – Krush Patel May 18 '22 at 20:58
  • for if any('R' in card for card in Player1Hand)? – Krush Patel May 18 '22 at 20:59
  • How would you do what? – Barmar May 18 '22 at 20:59
  • 1
    cards is a list of three sublists. Once you have removed the three sublists, cards becomes empty and random.choice() has nothing to pick from. – quamrana May 18 '22 at 20:59

1 Answers1

0
    if "R" in player1Hand and "B" in player2Hand:

should be

    if any("R" in card for card in player1Hand) and any("B" in card for card in player2Hand):

Since you use this so many times, I suggest you write a function for it.

def cardtype_in_hand(cardtype, hand):
    return any(cardtype in card for card in hand)

Then you can write:

if cardtype_in_hand('R', Player1Hand) and cardtype_in_hand('B', Player2Hand):

It also looks like the card type is always the first character, so you could change cardtype in card to card[0] == cardtype.

Barmar
  • 741,623
  • 53
  • 500
  • 612