Instructions
Requirements and Specifications
Source Code
from random import randint
cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J","Q","K","A"]
deck = []
# Take all elements from cards and add them to deck 4 times
for i in range(4):
deck += cards
user = []
comp = []
def draw_card() -> str:
"""
This function takes a card from the list 'deck' and returns it
:return: card -> str
"""
# randomly select index of card
index = randint(0, len(deck)-1)
# Get card and delete from deck
card = deck.pop(index)
# Return card
return card
def deal_cards() -> None:
"""
This function deals 7 cards for both user and computer. The cards are dealt alternatively to user, computer, user, computer, etc...
:return: None
"""
# Deal cards to user and computer
for i in range(7):
# Alternatively, deal one to user, then one to comp, etc
user.append(draw_card())
comp.append(draw_card())
def go_fish(hand: list) -> None:
"""
This function adds a new card to user or computer, depending of the hand passed as parameter
:param hand: List of cards (str)
:return: None
"""
# print statement
print("Go fish!")
# Check if the given hand is equal to user or comp
isUser = None
if hand == user:
isUser= True
else:
isUser = False
# Draw card
card = draw_card()
# Add to hand
hand.append(card)
# Sort hand
hand.sort()
# Display info to player
if isUser:
print("You drew a " + card)
else:
print("Computer drew a card")
def give_cards(giver: list, taker: list, card: str) -> tuple:
"""
This function checks if the card is in the giver's hand. If it is in the giver's hand, all instances
of that card are then removed from giver's hand and added to taker's hand
:param giver: list of giver's cards
:param taker: list of taker's cards
:param card: card
:return: Return the updated hands (lists) of giver and taker inside a tuple (giver, taker)
"""
print("The card " + card + " is being transferred.")
# Iterate through the cards in giver
for i, cardi in enumerate(giver):
if cardi == card: # The card in giver at index i is equal to card
# Remove from giver
del giver[i]
# Add to taker
taker.append(card)
Sort taker
taker.sort()
# Now return as tuple
return (giver, taker)
def display() -> None:
"""
This function shows to user the cards that he/she has in his/her hand, and displays the number
of cards in computer's hand
:return:None
"""
print("The user hand is: ", end="")
for i, card in enumerate(user):
print(card, end="")
if i < len(user)-1:
print(", ", end="")
print("")
print(f"Computer has {len(comp)} cards in its hand.")
def play_again() -> bool:
"""
This function asks to user of he/she wants to play again
The function uses an infinite loop that is broken only when the user enters a valid option
:return: None
"""
# Start infinite loop
while True:
option = input("Do you want to play? (Y/N): ")
if option.lower() == "y":
return True
elif option.lower() == "n":
return False
else:
print("Invalid input.")
def ask_user() -> None:
"""
This function asks to user for a card to be drawn from computer's hand
If the card is in computer's hand, then all instances of that card are added to user's hand and
removed from computer's hand
:return: None
"""
display()
# ask to user which card he/she wants to take from comp
card = input("Enter the card to be requested to the computer: ").upper()
# Check if the card is in computer's hand
if card in comp: # The comp has that card
giver, taker = give_cards(comp, user, card)
print("Computer has the card!")
else:
print("Computer does not have that card!")
go_fish(user)