Printing wrong python - python

I've recently gotten into coding, so I don't know a lot, which is why I am coming here for help. I am currently coding a game using python (Not a complicated game based off of what I am thinking of making) it's basically just a card game one of my friends made up. My only problem so far is one of my variables are returning wrong, and right at the same time?
This is the code that's being wonky:
Card = "Nothing"
def Draw_Card():
Rank = random.randint(1, 13)
print(Rank)
Card = str(Rank)
print(Card)
if Rank == 11:
Card = "J"
elif Rank == 12:
Card = "Q"
elif Rank == 13:
Card = "K"
Suit = random.randint(1, 4)
if Suit == 1:
Card = Card + "D"
elif Suit == 2:
Card = Card + "C"
elif Suit == 3:
Card = Card + "H"
elif Suit == 4:
Card = Card + "S"
Top()
Help()
Top()
print("And the game begins!")
time.sleep(2)
Top()
Card = "Nothing"
Draw_Card()
print(Card)
ignore the Top() functions and other functions, all that matters is the Draw_Card() and the print(Card) Running through this code will return
7,
7,
Nothing
assuming that 7 is the random value generated. Card is being printed right, when in the Draw_Card() function, but not when its outside the function. I dont understand why this is happening, and would apreciate some help
also another thing is the Card = "Nothing" doesnt work inside the function, it returns as Card is undefines, but thats besides the point

Inside Draw_Card, the name Card refers to a local variable. How about returning it?
At the end of the function, add
return Card
And outside the function, do:
Card = Draw_Card()
Another option would be to declare the name to be from the global scope:
def Draw_Card():
global Card
Rank = random.randint(1, 13)
print(Rank)
...
But I would generally avoid global and nonlocal if at all possible.

Assuming you want to print the suit and the card when you call the Draw_Card function (i.e. 7D), then you can move the print(card) to the bottom of the Draw_Card function.
Also in order to change the Card variable outside the function, you'd need to place global Card at the top of the function.
import random
Card = "Nothing"
def Draw_Card():
global Card
Rank = random.randint(1, 13)
Card = str(Rank)
if Rank == 11:
Card = "J"
elif Rank == 12:
Card = "Q"
elif Rank == 13:
Card = "K"
Suit = random.randint(1, 4)
if Suit == 1:
Card = Card + "D"
elif Suit == 2:
Card = Card + "C"
elif Suit == 3:
Card = Card + "H"
elif Suit == 4:
Card = Card + "S"
print(Card)
print("And the game begins!")
Card = "Nothing"
Draw_Card()
print(Card)

Your function just needs to return, you should focus on using return and then print the values returned
def Draw_Card():
Rank = random.randint(1, 13)
Card = str(Rank)
if Rank == 11:
Card = "J"
elif Rank == 12:
Card = "Q"
elif Rank == 13:
Card = "K"
Suit = random.randint(1, 4)
if Suit == 1:
Card = Card + "D"
elif Suit == 2:
Card = Card + "C"
elif Suit == 3:
Card = Card + "H"
elif Suit == 4:
Card = Card + "S"
return Card
print("And the game begins!")
Card = "Nothing"
Card = Draw_Card()
print(Card
And the game begins!
QS

Related

Blackjack python game

I have to create a blackjack game in python in which the user inputs the number of decks that are being used and the amount of money the user wants to bet. The rules are: o The player places his bet (should be read from the keyboard).
o The dealer and player are dealt two cards (one card of the dealer should be hidden).
o If the player has 21 he wins his bet; else if the dealer has 21 then the dealer wins and the player loses his bet.
o The player can only select to draw a new card (hit) or pass. Repeat until the player passes or busts. If the player goes above 21 then he busts.
o Then the dealer draws cards (automatically) until he either busts or reaches 17 or higher.
o Values of cards: A= 1 or 11 (the highest that does not bust you) and J=Q=K=10
o Note: in blackjack there are more actions: insurance, double, split; those are not required to be implemented as they would make the code you have to produce much, much longer and harder
I will show you my code below when I tried to run it, it said that the name "total" is not defined on the function game, but earlier on I have already defined it. I don't know how can i fix it.
import random
N=int(input("Please enter number of decks of cards used: "))
deck= [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*N
wins=0
losses=0
chip_pool = 100
bet = 1
def deal(deck):
hand = []
for i in range(2):
random.shuffle(deck)
card=deck.pop()
if card == 11:
card = "J"
if card == 12:
card = "Q"
if card == 13:
card = "K"
if card == 14:
card = "A"
hand.append(card)
return hand
def play_again():
again = input("Do you want to play again? (Y/N) : ").lower()
if again == "y":
dealer_hand = []
player_hand = []
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*N
game()
else:
print("Thank you for playing.")
exit()
def total_hand():
total = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
total = total + 10
elif card == "A":
if total >=11:
total = total + 1
else:
total = total + 11
else:
total = total + card
return total
def hit(hand):
card = deck.pop()
if card == 11:
card = "J"
if card == 12:
card = "Q"
if card == 13:
card = "K"
if card == 14:
card = "A"
hand.append(card)
return hand
def print_results(dealer_hand, player_hand):
clear()
print("\n WELCOME TO BLACKJACK!\n")
print("The dealer has a " + str(dealer_hand) + "for a total of" + str(total(dealer_hand)))
print("You have a" + str(player_hand) + "for a total of" + str(total(player_hand)))
def blackjack(dealer_hand, player_hand):
global wins
global losses
if total(player_hand) == 21:
print_results(player_hand,dealer_hand)
print("Congratulations! You got a blackjack.\n")
wins = wins + 1
play_again()
elif total(dealer_hand) == 21:
print_results(dealer_hand,player_hand)
print("Sorry you lose. The dealer got a blackjack.\n")
chip_pool -= bet
loses = loses + 1
play_again()
def score(dealer_hand,player_hand):
if total(player_hand) == 21:
print_results(dealer_hand, player_hand)
print("Congratulations! You got a blackjack1\n")
elif total(dealer_hand) == 21:
print_results(dealer_hand, player_hand)
print("Sorry you lose. The dealer go a blackjack\n")
elif total(player_hand) > 21:
print_results(dealer_hand, player_hand)
print("Sorry, you busted. You lose.\n")
elif total(dealer_hand) > 21:
print_results(dealer_hand, player_hand)
print("Dealer busts. You win!\n")
chip_pool += bet
elif total(player_hand) < total(dealer_hand):
print_results(dealer_hand, player_hand)
print("Sorry, the score is not higher than the dealer. You lose.\n")
chip_pool -= bet
elif total(player_hand) > total(dealer_hand):
print_results(dealer_hand, player_hand)
print("Congratulations. Your score is higher than the dealer. You win.\n")
chip_pool += bet
elif total(player_hand) == total(dealer_hand):
print_results(playe_hand, dealer_hand)
print("There is a tie. In a tie dealer always wins!\n")
chip_pool -= bet
def make_bet():
global bet
bet = 0
print("What amount of chips would you like to bet? ")
while bet == 0:
bet_comp = input()
bet_comp = int(bet_comp)
if bet_comp >= 1 and bet_comp <= chip_pool:
bet = bet_comp
else:
print("Invalid bet, you only have" + str(chip_pool) + "remaining")
def game():
choice = 0
print("Welcome to Blackjack!\n")
dealer_hand = deal(deck)
player_hand = deal(deck)
print("The dealer is showing a " +str(dealer_hand[0]))
make_bet()
print("You have a " + str(player_hand))
blackjack(dealer_hand, player_hand)
quit = False
while not quit:
choice = input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower()
if choice == 'h':
hit(player_hand)
print(player_hand)
if total(player_hand) > 21:
print("You busted")
chip_pool -= bet
play_again()
elif choice == "s":
while total(dealer_hand) < 17:
hit(dealer_hand)
print(dealer_hand)
if total(dealer_hand) > 21:
print("Dealer busts. You win!")
chip_pool += bet
play_again()
score(dealer_hand, playe_hand)
play_again()
elif choice == "q" :
print("Thank you for playing. Hope you enjoyed!")
quit = True
exit()
if __name__ == "__main__":
game()
You have not defined the function total that you call. Try adding this to your code
def total(array):
total = 0
for card in array:
if card == "J" or card == "Q" or card == "K":
total = total + 10
elif card == "A":
if total >=11:
total = total + 1
else:
total = total + 11
else:
total = total + card
return total
However I see some more issues in your code that will need fixing later on! Stand or Quit do nothing currently, and on busting there is an exception thrown since chip_pool never had a value assigned to it
EDIT 1:
You defined a function
def total_hand():
total = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
total = total + 10
elif card == "A":
if total >=11:
total = total + 1
else:
total = total + 11
else:
total = total + card
return total
Similar to what I suggested. Maybe it was just a typo but you need to do the following
Rename the function from total_hand to total OR call total_hand
Change the definition from total_hand() to total_hand(hand)

What could I return to continue my game, another Funcion or the answer?

Im coding a game called blackjack in py, but I've stop at this part.
Im trying to set this funcion on my code, the def end_game(), where return if the Player win or the Opponent win.
I don't know what I need return to player
import random
from art import logo
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_deck = []
opponent_deck = []
def table():
"""Table of cards for each player."""
print(f" Your cards: {player_deck}, current score: {sum(player_deck)}")
print(f" Computer's first card: {opponent_deck[0]}")
def add_card(player: list):
"""Add a new card on the current deck."""
player.append(random.choice(cards))
def deck(player: list):
"""Define the deck for a player, need to define the first argument as a list."""
for x in range(0,2):
add_card(player)
# Verify if the player just passed the number of cards necessary to play again.
def opponent_win():
print(f" Your final hand: {player_deck}, final score: {sum(player_deck)}")
print(f" Computer's final hand: {opponent_deck}, final score: {sum(opponent_deck)}")
print("You went over. You lose =(")
def player_win():
print(f" Your final hand: {player_deck}, final score: {sum(player_deck)}")
print(f" The computer's final hand: {opponent_deck}, final score: {sum(opponent_deck)}")
print("Opponent went over. You win =)")
# End Game
def end_game(player_decision):
"""Tell if the player or the opponent win"""
sum_player = sum(player_deck)
sum_opponent = sum(opponent_deck)
if sum_player == sum_opponent:
return print("Drawn")
elif sum_player == 21:
return player_win()
elif sum_player > 21:
return opponent_win()
elif sum_opponent > 21:
return player_win()
elif sum_opponent == 21:
return opponent_win()
if player_decision == "y":
add_card(player_deck)
if sum_opponent > sum_player:
return opponent_win()
elif sum_player > sum_opponent:
return player_win()
def start():
"""Start the blackjack game."""
# Show the logo
print(logo)
print("\n" * 50)
# Set your deck and the opponent deck and start the table
deck(player_deck)
deck(opponent_deck)
while sum(opponent_deck) < 21:
add_card(opponent_deck)
table()
while True:
another_card = input("Type 'y' to get another card, type 'n' to pass: ").lower()
end_game(another_card)
while True:
confirm = input("Do you want to play a game of blackjack? (y/n): ")
if confirm != "n":
start()
player_deck = []
opponent_deck = []
else:
break
I put an While loop to ask every time to player if he wants another card, and while this's doing I need to verify if the player overpass his hand and if the game end, the player ou the opponent wins.
If you are trying to stop the while loop, you need to do that outside of the end_game function
Make the function return a boolean to indicate if the game should continue
def end_game(player_decision):
"""Tell if the player or the opponent win"""
sum_player = sum(player_deck)
sum_opponent = sum(opponent_deck)
if sum_player == sum_opponent:
print("Drawn")
elif sum_player == 21:
player_win()
elif sum_player > 21:
opponent_win()
elif sum_opponent > 21:
player_win()
elif sum_opponent == 21:
opponent_win()
elif sum_opponent > sum_player:
opponent_win()
else:
player_win()
if player_decision == "y":
add_card(player_deck)
return True
return False
Then check it
continue_game = True
while continue_game:
another_card = input("Type 'y' to get another card, type 'n' to pass: ").lower()
continue_game = end_game(another_card)
But I'd recommend moving the another_card check outside of the end_game function since they are separate phases of the game. In which case, you don't need any return statements in end_game(), or any function parameter

Trying to program my computer player to be smarter in the card game "Go Fish". More specific info in body

'''
This is the card game "Go Fish", and I am trying to make my computer player smarter. As of right now, when player 2 asks for a card, it does so with the random.choice function like this (player_2_choice = random.choice(player_2_hand). I want to try and restrict the available choices by comparing player_2_hand to two different lists. One is for_player_2 = [], which is a collection of cards that player1 has asked for and player2 didn't have. If player2 didn't have the card, that means the card is still in player1's hand, so the computer player should know to ask for that card if they draw it later. The other list is remembered_cards = [], which is a collection of cards that player2 asks player1 for and player1 doesn't have the card in their hand, so player2 should know not to ask for that card for a set amount of turns because player2 knows that player1 doesn't have that card. Will post most of the program and comment as much as I can to help explain what I am trying to do. Just looking for suggestions on what I could do. Not allowed to use classes, thats part of another assignment. When making the comparisons between for_player_2 and player_2_hand, and remembered_cards and player_2_hand, I need to only compare the first letter of each element to find the match-Example: for_player_2 = ['2H'] player_2_hand = ['2C', '3C', '4c', 'KC', 'AC'] -- The check would see that 2H is for_player_2 and the 2C in player_2_hand match, so that would be the value stored in player_2_choice. Then that would be the card player_2 asks for.
'''
import random
deck = build_deck() #build_deck() builds a deck of cards
shuffle_deck(deck) #shuffles deck
player_1_hand = deal_hand(deck, 7) #Deals 7 cards to player1, card example: 'AS'
player_2_hand = deal_hand(deck, 7) #Deals 7 cards to player2
player_1_pairs = remove_pairs(player_1_hand) #Removes pairs at beginning of game
player_2_pairs = remove_pairs(player_2_hand) #Removes pairs at beginning of game
for_player_2 = [] # Will be added to later
remembered_cards = [] #Will be added to later
while True:
if len(player_1_hand) == 0 or len(player_2_hand) == 0 or len(deck) == 0:
break
# Player 1's turn
while True:
if len(player_1_hand) == 0:
break
print("Your cards ", player_1_hand)
desired_card = input("What card would you like from your opponent?")
if desired_card not in player_1_hand:
print("Invalid option, please choose again.")
continue
if check_guess(player_2_hand, player_1_hand, desired_card, player_1_pairs) == True:
#This function above checks to see if the guess is in the other player's hand
print("Well done!!!")
else:
print("Go Fish")
player_1_hand.append(deal_top_card(deck)) #Deals top card of deck to player
d_card = player_1_hand[-1] #Last card in hand is the card we just drew
# Since we had to go fish, the desired card is added to the list for_player_2
for_player_2.append(desired_card)
if check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == True:
print("Congratulations, you drew the match to your pair!!!")
continue
elif check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == False:
for i in range(len(player_1_hand)):
for j in range(i + 1, len(player_1_hand)):
# If card ranks match
card1 = player_1_hand[i]
card2 = player_1_hand[j]
if card1[0] == card2[0]:
#Add the pairs to the player pair list
player_1_pairs.extend([card1, card2])
#Remove pairs from player's hand
player_1_hand.remove(card1)
player_1_hand.remove(card2)
break
print(f"Player 1 has {len(player_1_hand)} cards in their hand")
break
#Player 2's turn
while True:
if len(player_2_hand) == 0:
break
#This is the section I am trying to figure out mainly
#Want check and see if matching element between for_player_2 and player_2_hand
cardFound = False
for card in for_player_2:
for hand_card in player_2_hand:
if hand_card[0] == card[0]:
player_2_choice = card
print("Player 2 asked for ", card)
cardFound = True
break
if not cardFound:
#This is where the comparison needs to be made between
#remembered_cards and player_2_hand
player_2_choice = random.choice(player_2_hand) # This is original but I want to change
print("Player 2 asked for", player_2_choice) # Original
#After first check between for_player_2 and player_2_hand I want to check another
#Next I want to check if there is a matching element between remembered_cards
#And player_2_hand because remembered_cards are the cards player2 asked for
#and player1 didn't have, so player2 shouldn't ask for that card again for
#a set amount of turns. Not sure if that is possible.
if player_2_choice not in player_2_hand:
continue
if check_guess(player_1_hand, player_2_hand, player_2_choice, player_2_pairs) == True:
print("Player 2 got a pat on the back for guessing correctly!!!")
else:
print("Player 2 had to go fishing!!!")
#Here is where the failed player_2_choice gets added to remembered cards
remembered_cards.append(player_2_choice)
player_2_hand.append(deal_top_card(deck)) #Deals top card of deck to player2
d_card = player_2_hand[-1]
if check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == True:
print("Player 2 got a pat on the back for drawing the match to their pair")
continue
elif check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == Fals:
for i in range(len(player_2_hand)):
for j in range(i + 1, len(player_2_hand)):
#If card ranks match
card1 = player_2_hand[i]
card2 = player_2_hand[j]
if card1[0] == card2[0]:
player_2_pairs.extend([card1, card2])
player_2_hand.remove(card1)
player_2_hand.remove(card2)
break
print(f"Player 2 has {len(player_2_hand)} cards in their hand")
break
import random
deck = build_deck() #build_deck() builds a deck of cards
shuffle_deck(deck) #shuffles deck
player_1_hand = deal_hand(deck, 7) #Deals 7 cards to player1, card example: 'AS'
player_2_hand = deal_hand(deck, 7) #Deals 7 cards to player2
player_1_pairs = remove_pairs(player_1_hand) #Removes pairs at beginning of game
player_2_pairs = remove_pairs(player_2_hand) #Removes pairs at beginning of game
for_player_2 = [] # Will be added to later
remembered_cards = {} # <========== converted to a Set for better preformance
while True:
if len(player_1_hand) == 0 or len(player_2_hand) == 0 or len(deck) == 0:
break
# Player 1's turn
while True:
if len(player_1_hand) == 0:
break
print("Your cards ", player_1_hand)
desired_card = input("What card would you like from your opponent?")
if desired_card not in player_1_hand:
print("Invalid option, please choose again.")
continue
if check_guess(player_2_hand, player_1_hand, desired_card, player_1_pairs) == True:
#This function above checks to see if the guess is in the other player's hand
print("Well done!!!")
else:
print("Go Fish")
player_1_hand.append(deal_top_card(deck)) #Deals top card of deck to player
d_card = player_1_hand[-1] #Last card in hand is the card we just drew
# Since we had to go fish, the desired card is added to the list for_player_2
for_player_2.append(desired_card)
if check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == True:
print("Congratulations, you drew the match to your pair!!!")
continue
elif check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == False:
for i in range(len(player_1_hand)):
for j in range(i + 1, len(player_1_hand)):
# If card ranks match
card1 = player_1_hand[i]
card2 = player_1_hand[j]
if card1[0] == card2[0]:
#Add the pairs to the player pair list
player_1_pairs.extend([card1, card2])
#Remove pairs from player's hand
player_1_hand.remove(card1)
player_1_hand.remove(card2)
break
print(f"Player 1 has {len(player_1_hand)} cards in their hand")
break
#Player 2's turn
while True:
if len(player_2_hand) == 0:
break
#This is the section I am trying to figure out mainly
#Want check and see if matching element between for_player_2 and player_2_hand
cardFound = False
for card in for_player_2:
for hand_card in player_2_hand:
if hand_card[0] == card[0]:
player_2_choice = card
# not_needed #print("Player 2 asked for ", card)
cardFound = True
break
if not cardFound:
#This is where the comparison needs to be made between
#remembered_cards and player_2_hand
player_2_choice = random.choice( # random choose
list( # random.choice requires list (as an argument)
set(player_2_hand).\ # convert player_2_hand to a Set to use difference method
difference(remembered_cards) # remove cards that is in remembered_cards
) # randomly choose from the result list
)
# not_needed #player_2_choice = random.choice(player_2_hand) # This is original but I want to change
print("Player 2 asked for", player_2_choice) # Original
#After first check between for_player_2 and player_2_hand I want to check another
#Next I want to check if there is a matching element between remembered_cards
#And player_2_hand because remembered_cards are the cards player2 asked for
#and player1 didn't have, so player2 shouldn't ask for that card again for
#a set amount of turns. Not sure if that is possible.
if player_2_choice not in player_2_hand:
continue
if check_guess(player_1_hand, player_2_hand, player_2_choice, player_2_pairs) == True:
print("Player 2 got a pat on the back for guessing correctly!!!")
else:
print("Player 2 had to go fishing!!!")
#Here is where the failed player_2_choice gets added to remembered cards
remembered_cards.add(player_2_choice)
player_2_hand.append(deal_top_card(deck)) #Deals top card of deck to player2
d_card = player_2_hand[-1]
if check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == True:
print("Player 2 got a pat on the back for drawing the match to their pair")
continue
elif check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == Fals:
for i in range(len(player_2_hand)):
for j in range(i + 1, len(player_2_hand)):
#If card ranks match
card1 = player_2_hand[i]
card2 = player_2_hand[j]
if card1[0] == card2[0]:
player_2_pairs.extend([card1, card2])
player_2_hand.remove(card1)
player_2_hand.remove(card2)
break
print(f"Player 2 has {len(player_2_hand)} cards in their hand")
break

I dont know how to go about looping a specific part of my code

I'm writing a casino-based game and I'm having some trouble with coding blackjack, I run into a problem where you only have the option to "hit" once, and I'm not sure how to make it loop. Once you've "hit" it just settles with your score as if it was final even tho you might still be far under 21. Every time I try to fix it some other part of the code just breaks.
(keep in mind this is not the full code but just the blackjack part)
import os
import random
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
bal = 100
balstr = str(bal) + "$"
def clear():
os.system('cls')
def deal(deck):
hand = []
for i in range(2):
random.shuffle(deck)
card = deck.pop()
if card == 11:card = "J"
if card == 12:card = "Q"
if card == 13:card = "K"
if card == 14:card = "A"
hand.append(card)
return hand
def newRound():
again = input("Do you want to play again? (Y/N): ").lower()
if again == "y":
blackjack()
else:
#takes you back to main menu in the full code, just ignore this
position()
def total(hand):
total = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
total+= 10
elif card == "A":
if total >= 11:
total+= 1
else: total+= 11
else:
total += card
return total
def hit(hand):
card = deck.pop()
if card == 11:
card = "J"
if card == 12:
card = "Q"
if card == 13:
card = "K"
if card == 14:
card = "A"
hand.append(card)
return hand
def currentHands(dealerHand, playerHand):
clear()
print(("The dealer has a ") + str(dealerHand) + " for a total of " + str(total(dealerHand)))
print(("You have a ") + str(playerHand) + " for a total of " + str(total(playerHand)))
def score(dealerHand, playerHand, usrbetint):
global bal
if total(playerHand) == 21 or total(dealerHand) > 21 or total(playerHand) > total(dealerHand) and total(playerHand) < 21:
currentHands(dealerHand, playerHand)
bal += usrbetint
print("Congratulations, you win!\n \nYour new balance is {}$".format(bal))
else :
currentHands(dealerHand, playerHand)
bal -= usrbetint
print("Sorry, you lose.\n \nYour new balance is {}$".format(bal))
def blackjack():
choice = 0
clear()
print("Let's play blackjack!\n")
userbet = input("(for help type help) How much money do you want to use: ").upper()
if userbet == "HELP" :
if userbet == "HELP" :
print("Instructions")
else :
print("Something went wrong")
pass
else :
usrbetint = int(userbet)
dealerHand = deal(deck)
dealerHandShow = [dealerHand[0]]
dealerHandShow = total(dealerHandShow)
playerHand = deal(deck)
print(("The dealer is showing a ") + str(dealerHand[0]) + " for a total of " + str(dealerHandShow))
print(("You have a ") + str(playerHand) + " for a total of " + str(total(playerHand)))
choice = input("Do you want to [H]it or [S]tand?: ").lower()
clear()
if choice == "h":
hit(playerHand)
while total(dealerHand) < 17:
hit(dealerHand)
score(dealerHand, playerHand, usrbetint)
newRound()
elif choice == "s":
while total(dealerHand) < 17:
hit(dealerHand)
score(dealerHand, playerHand, usrbetint)
newRound()
blackjack()
i assume the fix would be somewhere around the last 20 lines of the "blackjack" function but didnt know how to explain everything without sending the clump of code.
If someone please could give me tips on where to change stuff i'd really appreciate that and ignore the "global bal" part, it was the only way i knew to add a truly global variable.
Since you don't know how many times you'll be looping (it is based on user input), you probably want a while loop. Your current code mixes the dealer behavior into the player handling, you you'll need to separate that out, since we don't want to loop it.
print("You have a " + str(playerHand) + " for a total of " + str(total(playerHand)))
choice = input("Do you want to [H]it or [S]tand?: ").lower()
while choice == "h":
clear()
hit(playerHand)
print("You have a " + str(playerHand) + " for a total of " + str(total(playerHand)))
choice = input("Do you want to [H]it or [S]tand?: ").lower()
while total(dealerHand) < 17:
hit(dealerHand)
score(dealerHand, playerHand, usrbetint)
You might want to add an additional condition to stop asking the player if they want to hit when they've already busted.

List failing on second run

I am currently trying to program a mathematical card trick, which asks the user what pile their random card is in. However, the second time it runs (the 'trick phase' has to occur 3 times for the trick to work) the list index becomes out of range. I am unsure where exactly the problem lies and will attach the current version so you can try to run it to see clearer. Thanks!
import random
def makedeck():
listy = []
cardsindeck = 0
while cardsindeck != 21:
suit = random.randint(1,4)
if suit == 1:
suit = "D"
elif suit == 2:
suit = "H"
elif suit == 3:
suit = "S"
else:
suit = "C"
cardtype = random.randint(1,13)
if cardtype == 1:
card = "A"
elif cardtype == 2:
card = "2"
elif cardtype == 3:
card = "3"
elif cardtype == 4:
card = "4"
elif cardtype == 5:
card = "5"
elif cardtype == 6:
card = "6"
elif cardtype == 7:
card = "7"
elif cardtype == 8:
card = "8"
elif cardtype == 9:
card = "9"
elif cardtype == 10:
card = "10"
elif cardtype == 11:
card = "J"
elif cardtype == 12:
card = "Q"
else:
card = "K"
cardandsuit = (card + suit)
if cardandsuit not in listy:
listy.append(cardandsuit)
cardsindeck = cardsindeck + 1
return listy
def dealdeck(listy):
list1 = []
list2 = []
list3 = []
for i in range(len(listy)):
if i % 3 == 0:
list1.append(listy[i])
elif i % 3 == 1:
list2.append(listy[i])
else:
list3.append(listy[i])
return[list1, list2, list3]
def makepiles(pile1,pile2,pile3):
print("Pile 1\t\tPile 2\t\t Pile 3\t\t")
for i in range(7):
print(pile1[i],"\t\t",pile2[i],"\t\t",pile3[i],"\t\t")
def usercardpile():
userinput = input("What pile is your card in?")
if userinput == "1" or userinput.title() == "One":
return 1
elif userinput == "2" or userinput.title() == "Two":
return 2
elif userinput == "3" or userinput.title() == "Three":
return 3
else:
print("Please only write 1, 2 or 3")
return usercardpile()
listy = makedeck()
pile1, pile2, pile3 = dealdeck(listy)
for i in range(1,4):
newlisty = makepiles(pile1,pile2,pile3)
userspile = usercardpile()
if userspile == 1:
newlisty = (pile2,pile1,pile3)
elif userspile == 2:
newlisty = (pile1,pile2,pile3)
else:
newlisty = (pile1,pile3,pile2)
pile1, pile2, pile3 = dealdeck(newlisty)
print("Your card is",newlisty[10])
One issue is with this line of code:
newlisty = makepiles(pile1, pile2, pile3)
You're expecting makepiles to return 3 lists, whereas it returns None (no explicit item returned).
I imagine if you were to return the piles from that function, it'd work.
The other thing is, You are doing this:
newlisty = (pileX, pileY, pileZ)
This will create a tuple of lists, and you will iterate over the entire lists rather than the individual cards. I believe you want
newlisty = pile1 + pile3 + pile2`
This'll create a composite list of 21 elements by linearly combining the smaller piles.
Other comments:
Consider storing your decktype and card type in dicts. That way, you can quickly lookup and generate your piles without having to write a long set of if statements. Example:
You can reduce
cardtype = random.randint(1,13)
if cardtype == 1:
card = "A"
elif cardtype == 2:
card = "2"
elif cardtype == 3:
card = "3"
elif cardtype == 4:
card = "4"
elif cardtype == 5:
card = "5"
elif cardtype == 6:
card = "6"
elif cardtype == 7:
card = "7"
elif cardtype == 8:
card = "8"
elif cardtype == 9:
card = "9"
elif cardtype == 10:
card = "10"
elif cardtype == 11:
card = "J"
elif cardtype == 12:
card = "Q"
else:
card = "K"
To...
cardtype_lookup = { 1 : 'A', 2 : '2', 3 : '3', .... 12 : 'K' }
card = cardtype_lookup[random.randint(1, 13)]
...And so on.
I think you are hitting a few issues with your code beyond just the final iterations. Below are some suggestions along with comments that I believe accomplishes your purpose.
from random import shuffle
# Function for generating 21 random cards
def makedeck():
# Create 52 cards by suit and cardtype
listy = [card + suit for card in ['A','2','3','4','5','6','7','8','9','10','J','Q','K'] for suit in ['D','H','S','C']]
# Shuffle the list
shuffle(listy)
# Choose only the first 21 items of that list
listy = listy[:21]
return listy
# Function for dividing 21-card deck into 3 equally-sized piles
def dealdeck(listy):
# Iterate over listy, stepping by 3, starting with the first, second, and third item
list1 = listy[::3]
list2 = listy[1::3]
list3 = listy[2::3]
# Return the three lists as three items to correspond to the three piles of the call
return list1, list2, list3
# This works
def makepiles(pile1,pile2,pile3):
print("Pile 1\t\tPile 2\t\t Pile 3\t\t")
for i in range(7):
print(pile1[i],"\t\t",pile2[i],"\t\t",pile3[i],"\t\t")
# This works
def usercardpile():
userinput = input("What pile is your card in?")
if userinput == "1" or userinput.title() == "One":
return 1
elif userinput == "2" or userinput.title() == "Two":
return 2
elif userinput == "3" or userinput.title() == "Three":
return 3
else:
print("Please only write 1, 2 or 3")
return usercardpile()
listy = makedeck()
pile1, pile2, pile3 = dealdeck(listy)
for i in range(1,4):
# Because this function does not return anything, it should be run on its own, and not assigned to a variable
makepiles(pile1,pile2,pile3)
userspile = usercardpile()
# Now you want to re-order the piles based on user input. Because these are lists, you can simply add them together in a new order to create a newly arranged list
if userspile == 1:
newlisty = pile2 + pile1 + pile3
elif userspile == 2:
newlisty = pile1 + pile2 + pile3
else:
newlisty = pile1 + pile3 + pile2
# Now you create new piles based on the re-sorted list and re-iterate
pile1, pile2, pile3 = dealdeck(newlisty)
# Uses .format method instead
print("Your card is {}".format(newlisty[10]))

Categories

Resources