Question: Can the following code be modified to allow the user to pick 1 card and have the program determine the secret card? The program works,

Can the following code be modified to allow the user to pick 1 card and have the program determine the secret card? The program works, but it is having trouble determining the secret card. Please keep psudedocode and variables the same. Thanks!
#CardTrick.py
import random
def main():
#declare variables
column =0
loop_counter =0
#declare the decklist
deck =[0]*52
#declare a 7 row by 3 column array
cols =3
rows =7
play =[[0 for _ in range(cols)] for _ in range(rows)]
#Opening Message
print("Welcome. Im am not the latest development in AI, but,")
print("I'm a computer program that can perform a card trick")
print("Lets begin!
")
print("Building the deck of cards...")
#Call Build Deck()
BuildDeck(deck)
print("Deck built successfully!
")
see_deck = input("Would you like to see the full deck (y/n)?")
if see_deck.lower()=='y':
#Call Print Deck()
PrintDeck(deck)
#Begin the Main Card Trick Loop
for loop_counter in range(3):
Deal(deck, play)
column = int(input("Which column is your card in (0,1, or 2)?: "))
PickUp(deck, play, column)
# Call SecretCard()
SecretCard(deck)
print("Thank you for playing the card trick!")
def BuildDeck(deck):
#define local variables
used =[0]*52
i =0
# Generate cards while the deck is full of intergers
while i <52:
# Generate a random interger between 0 and 51.
card = random.randint(0,51)
# Test to see if the value has already been used.
# If not, add it to the deck
if used[card]==0:
used[card]=1
deck[i]= card
i +=1
#End while loop
#Call PrintCard()
def PrintCard(card):
rank = card %13
suit = card //13
# Define Ranks
if rank ==0:
cardString = "King of "
elif rank ==1:
cardString = "Ace of "
elif rank ==11:
cardString = "Jack of "
elif rank ==12:
cardString = "Queen of "
else:
cardString = f"{rank} of "
# Define Suits
if suit ==0:
cardString += "Clubs"
elif suit ==1:
cardString += "Diamonds"
elif suit ==2:
cardString += "Hearts"
else:
cardString += "Spades"
print(f"{cardString: <20}", end='')
# Call PrintDeck()
def PrintDeck(deck):
for i in range(52):
PrintCard(deck[i])
if (i +1)%3==0:
print()
# end for loop
def Deal(deck, play):
# Define local variables
card =0
# Deal the cards from the deck to the play list
print("
Column 0 Column 1 Column 2")
print("===================================================")
#Begin nested for loop structure to deal out the cards
for row in range(7):
for column in range(3):
play[row][column]= deck[card]
PrintCard(play[row][column])
card +=1
print()
#end nested for loop structure
#Call PickUp()
def PickUp(deck, play, column):
# Define local variables
card =0
order =[column,(column +1)%3,(column +2)%3]
#Identify the order of columns and rows to pick up
for col in order:
for row in range(7):
deck[card]= play[row][col]
card +=1
# End for loop structure
def SecretCard(deck):
print("
Finding Secret Card...")
for card in range(0,10):
PrintCard(deck[card])
card +=1
print()
print("
Your secret card is: ", end='')
PrintCard(deck[10])
print()
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!