Help fix this code. Exploding kittens card came in python.
import random
# --------------------------------------------------------------------------------------------------------------------------------
------------
# SET UP
# create deck
deck = ['see the future'] * 5 + ['attack'] * 4 + ['skip'] * 4 + ['favor'] * 4 + ['nope'] * 5 +
['shuffle'] * 4 + ['taco cat'] * 4 + ['harry potato cat'] * 4 + ['beard cat'] * 4 + ['catermelon'] * 4 +
['rainbow ralphing cat'] * 4
# dealing a card from the deck
def deal_card(deck):
return deck.pop()
# adding a card to a hand
def add_card_to_hand(hand, card):
hand.append(card)
# function to draw a card from the deck
def draw_card(deck):
if deck:
return deck.pop(0)
else:
return None
# shuffle the deck
random.shuffle(deck)
# Welcome message
print("nWelcome to Exploding Kittens!n")
# ask user for how many players
num_players = int(input("nThere can be 2-5 players. How many players will be playing?n"))
# Check if the number of players is between 2 and 5
while num_players < 2 or num_players > 5:
print("Invalid number of players.")
num_players = int(input("nThere can be 2-5 players. How many players will be playing?n"))
if num_players < 2 or num_players > 5:
print("nInvalid optionn")
else:
players = []
for i in range(num_players):
player = {
"id": i + 1,
"hand": [],
"active": True,
"lives": 1
}
players.append(player)
# adding a defuse card to each player's hand
for j in range(1):
add_card_to_hand(player["hand"], 'defuse')
# dealing 7 more cards to each player
for j in range(7):
card = deal_card(deck)
add_card_to_hand(player["hand"], card)
# adding exploding kittens back into the deck based on num_players
for i in range(num_players - 1):
deck.append('exploding kitten')
# adding defuse back into the deck based on num_players
if num_players == 2 or num_players == 3 or num_players == 4:
deck.append('defuse')
deck.append('defuse')
elif num_players == 5:
deck.append('defuse')
# shuffle the deck
random.shuffle(deck)
# --------------------------------------------------------------------------------------------------------------------------------
------------
# CARDS
# ATTACK
def attack(player_hands, player_num, deck):
# ask the current player who they want to attack
target_num = int(input("Who do you want to attack?"))
# make the attacked player draw two cards from the deck
for i in range(2):
card = draw_card(deck)
if card == "exploding kitten":
if "defuse" in player_hands[target_num]:
# the attacked player has a defuse card
print(f"Player {target_num} drew an exploding kitten but defused it!")
player_hands[target_num].remove("defuse")
# reinsert the exploding kitten card back into the deck
deck.insert(random.randint(0, len(deck)), "exploding kitten")
else:
# the attacked player doesn't have a defuse card and is out of the game
print(f"Player {target_num} drew an exploding kitten and is out of the game.")
else:
add_card_to_hand(player_hands[target_num], card)
print(f"Player {target_num} has drawn 2 cards from the deck.")
# .
Help fix this code Exploding kittens card came in python .pdf
1. Help fix this code. Exploding kittens card came in python.
import random
# --------------------------------------------------------------------------------------------------------------------------------
------------
# SET UP
# create deck
deck = ['see the future'] * 5 + ['attack'] * 4 + ['skip'] * 4 + ['favor'] * 4 + ['nope'] * 5 +
['shuffle'] * 4 + ['taco cat'] * 4 + ['harry potato cat'] * 4 + ['beard cat'] * 4 + ['catermelon'] * 4 +
['rainbow ralphing cat'] * 4
# dealing a card from the deck
def deal_card(deck):
return deck.pop()
# adding a card to a hand
def add_card_to_hand(hand, card):
hand.append(card)
# function to draw a card from the deck
def draw_card(deck):
if deck:
return deck.pop(0)
else:
return None
# shuffle the deck
random.shuffle(deck)
# Welcome message
print("nWelcome to Exploding Kittens!n")
# ask user for how many players
num_players = int(input("nThere can be 2-5 players. How many players will be playing?n"))
# Check if the number of players is between 2 and 5
while num_players < 2 or num_players > 5:
print("Invalid number of players.")
num_players = int(input("nThere can be 2-5 players. How many players will be playing?n"))
if num_players < 2 or num_players > 5:
print("nInvalid optionn")
else:
players = []
for i in range(num_players):
player = {
"id": i + 1,
"hand": [],
"active": True,
"lives": 1
}
2. players.append(player)
# adding a defuse card to each player's hand
for j in range(1):
add_card_to_hand(player["hand"], 'defuse')
# dealing 7 more cards to each player
for j in range(7):
card = deal_card(deck)
add_card_to_hand(player["hand"], card)
# adding exploding kittens back into the deck based on num_players
for i in range(num_players - 1):
deck.append('exploding kitten')
# adding defuse back into the deck based on num_players
if num_players == 2 or num_players == 3 or num_players == 4:
deck.append('defuse')
deck.append('defuse')
elif num_players == 5:
deck.append('defuse')
# shuffle the deck
random.shuffle(deck)
# --------------------------------------------------------------------------------------------------------------------------------
------------
# CARDS
# ATTACK
def attack(player_hands, player_num, deck):
# ask the current player who they want to attack
target_num = int(input("Who do you want to attack?"))
# make the attacked player draw two cards from the deck
for i in range(2):
card = draw_card(deck)
if card == "exploding kitten":
if "defuse" in player_hands[target_num]:
# the attacked player has a defuse card
print(f"Player {target_num} drew an exploding kitten but defused it!")
player_hands[target_num].remove("defuse")
# reinsert the exploding kitten card back into the deck
deck.insert(random.randint(0, len(deck)), "exploding kitten")
else:
# the attacked player doesn't have a defuse card and is out of the game
print(f"Player {target_num} drew an exploding kitten and is out of the game.")
else:
add_card_to_hand(player_hands[target_num], card)
print(f"Player {target_num} has drawn 2 cards from the deck.")
3. # FAVOR
def favor(player_hands, player_num):
# get the list of players' ids except for the current player
player_ids = [p['id'] for p in players if p['id'] != player_num]
# check if there are any other players
if not player_ids:
print("There are no other players to take a card from.")
return
# ask the current player which player they want to take a card from
print("Which player would you like to take a card from?")
for i, pid in enumerate(player_ids):
print(f"{i+1}. Player {pid}")
choice = int(input()) - 1
# get the chosen player's hand
chosen_player_hand = player_hands[player_ids[choice] - 1]
# check if the chosen player has any cards
if not chosen_player_hand:
print(f"Player {player_ids[choice]} has no cards to take.")
return
# choose a random card from the chosen player's hand
card_to_take = random.choice(chosen_player_hand)
chosen_player_hand.remove(card_to_take)
# add the card to the current player's hand
add_card_to_hand(player_hands[player_num-1], card_to_take)
print(f"You took '{card_to_take}' from player {player_ids[choice]}.")
# SEE THE FUTURE
def see_the_future(player_hands, player_num, pick_card, deck):
# Check if the card is in the player's hand
if len(deck) > 3:
print("The card '{0}' has been removed from player {1}'s hand.".format(pick_card, player_num))
# Display the top three cards
print("nThe top three cards of the deck are:n")
# last three numbers in the array is really where player will draw from
for i in range(-1, -4, -1):
print(deck[i])
# check if deck has three cards to diplay
else:
print("There are not three cards to view in the deck")
# SKIP
def skip(player_num, pick_card, player_hands):
print("Turn skipped.n")
# SHUFFLE
4. def shuffle_fun(deck, player, players):
random.shuffle(deck)
print("The deck has been shuffled.n")
# CAT CARDS
def cat_cards(deck, player, players):
print ("cat card")
# --------------------------------------------------------------------------------------------------------------------------------
------------
# GAME PLAY
# until there is only one player left
while len([p for p in players if p['active']]) > 1:
# iterate over all players
for player in players:
# skip inactive players
if not player['active']:
continue
# display player's hand
print("nPlayer {0}, here is your hand:".format(player['id']))
for card in player['hand']:
print(card)
# prompt user how many cards they want to play
num_cards = int(input("nPlayer {0}, how many cards do you want to play? (0 to
pass)n".format(player['id'])))
# play cards
for i in range(num_cards):
# prompt user to choose a card
card_name = input("nChoose a card to play:n")
# check if card is in player's hand
if card_name not in player['hand']:
print("You do not have {0} in your hand.".format(card_name))
continue
elif card_name == "attack":
attack('player_hands', 'player_num', deck)
elif card_name == "favor":
favor('player_hands', 'player_num', 'pick_card', deck)
elif card_name == "skip":
skip('player_num', 'pick_card', 'player_hands')
elif card_name == "shuffle":
shuffle_fun(deck, player, players)
elif card_name == "see the future":
see_the_future('player_hands', 'player_num', 'pick_card', deck)
elif card_name == "taco cat" or card_name == "beard cat" or card_name == "catermelon" or
5. card_name == "rainbow ralphing cat" or card_name == "harry potato cat":
cat_cards(deck, player, players)
player['hand'].remove(card_name)
if card_name != 'skip' or card_name != 'attack':
# draw card
card = draw_card(deck)
add_card_to_hand(player['hand'], card)
if card == 'exploding kitten':
if 'defuse' in player['hand']:
print ("You drew an exploding kitten, but you had a defuse to save you!")
# discard defuse
player['hand'].remove('defuse')
# place exploding kitten back into the deck at a random spot
index = random.randint(0, len(deck))
deck.insert(index, 'exploding kitten')
# remove exploding kitten from hand
player['hand'].remove('exploding kitten')
else:
print("Oh no! You drew an exploding kitten and lost a life.")
player_status[player_num-1] = False
print("You're out of the game!")
# get list of active players
active_players = [p for p in players if p['active']]
# check if only one active player left
if len(active_players) == 1:
# get the active player
active_player = [p for p in players if p['active']][0]
print("Player {0} wins!".format(active_player['id']))
else:
print("You drew a {0}.".format(card))
# draw a card if the player chose to pass
if num_cards == 0:
card = draw_card(deck)
add_card_to_hand(player['hand'], card)
if card == 'exploding kitten':
if 'defuse' in player['hand']:
print ("You drew an exploding kitten, but you had a defuse to save you!")
# discard defuse
player['hand'].remove('defuse')
# place exploding kitten back into the deck at a random spot
index = random.randint(0, len(deck))
deck.insert(index, 'exploding kitten')
6. # remove exploding kitten from hand
player['hand'].remove('exploding kitten')
else:
print("Oh no! You drew an exploding kitten and lost a life.")
player_status[player_num-1] = False
print("You're out of the game!")
# get list of active players
active_players = [p for p in players if p['active']]
# check if only one active player left
if len(active_players) == 1:
# get the active player
active_player = [p for p in players if p['active']][0]
print("Player {0} wins!".format(active_player['id']))
else:
print("You drew a {0}.".format(card))
print ("Game Over!")