I have a serious question for python peeps

#1

so this isnt an exercise, but something im working on individually. its a game called 31 but essentially blackjack and my discard pile does not work

in my code, i write if this card is in the players hand, then replace the discard card for whatever card they want to replace

instead, what happens is the card says its never in the hand when it clearly is

``````Replace this line with your code.

import random

#creating deck of 52 cards
def make_deck():
hearts = ["2 ♥", "3 ♥", "4 ♥", "5 ♥", "6 ♥", "7 ♥", "8 ♥", "9 ♥", "10 ♥","J ♥", "Q ♥", "K ♥", "A ♥"]
spades = ["2 ♠", "3 ♠", "4 ♠", "5 ♠", "6 ♠", "7 ♠", "8 ♠", "9 ♠", "10 ♠","J ♠", "Q ♠", "K ♠", "A ♠"]
diamonds = ["2 ♦", "3 ♦", "4 ♦", "5 ♦", "6 ♦", "7 ♦", "8 ♦", "9 ♦", "10 ♦","J ♦", "Q ♦", "K ♦", "A ♦"]
clubs = ["2 ♣", "3 ♣", "4 ♣", "5 ♣", "6 ♣", "7 ♣", "8 ♣", "9 ♣", "10 ♣", "J ♣", "Q ♣", "K ♣", "A ♣"]
deck = hearts + spades + diamonds + clubs
return deck

#creating hand of 3 hands
def make_hand(deck):
hand = random.sample(deck, 3)
return hand

#creating hand 1
bothHands = []
def hands(deck):
hand = make_hand(deck)
bothHands.append(hand)
print (currentPlayer, "your hand is", hand)
return hand
return bothHands

#getting rid of cards in players hand from deck
#putting cards in players hand into list called used_cards
used_Cards = []
def used_cards(deck, cardHand):

for card in cardHand:
deck.remove(card)
used_Cards.append(card)
print ("used cards are", used_Cards)
return used_Cards
return deck

#counting score of players
def score_hand(cardHand):
splitValue = [value.split(" ", 1) [0] for value in cardHand]
splitSuit= [suit.split(" ", 1) [1] for suit in cardHand]
print (splitValue, splitSuit)

numHearts = []
numDiamonds = []
numClubs = []

for count in range(3):
if splitValue[count] in "KQJ" and splitSuit[count] == "♥":
numHearts.append(10)
elif splitValue[count] in "KQJ" and splitSuit[count] == "♠":
elif splitValue[count] in "KQJ" and splitSuit[count] == "♦":
numDiamonds.append(10)
elif splitValue[count] in "KQJ" and splitSuit[count] == "♣":
numClubs.append(10)
if splitValue[count] == "A" and splitSuit[count] == "♥":
numHearts.append(11)
elif splitValue[count] == "A" and splitSuit[count] == "♠":
elif splitValue[count] == "A" and splitSuit[count] == "♦":
numDiamonds.append(11)
elif splitValue[count] == "A" and splitSuit[count] == "♣":
numClubs.append(11)
if splitSuit[count] == "♥" and splitValue[count] in "2345678910":
numHearts.append(splitValue[count])
elif splitSuit[count] == "♠" and splitValue[count] in "2345678910":
elif splitSuit[count] == "♦" and splitValue[count] in "2345678910":
numDiamonds.append(splitValue[count])
elif splitSuit[count] == "♣" and splitValue[count] in "2345678910":
numClubs.append(splitValue[count])

numHearts = [int(i) for i in numHearts]
numDiamonds = [int(i) for i in numDiamonds]
numClubs = [int(i) for i in numClubs]

numHearts = sum(numHearts)
numDiamonds = sum(numDiamonds)
numClubs = sum(numClubs)

points = 0

if numHearts > numSpades and numHearts > numDiamonds and numHearts > numClubs:
points = numHearts
print (currentPlayer, "has", points, "points")
print (currentPlayer, "has", points, "points")
elif numDiamonds > numSpades and numDiamonds > numHearts and numDiamonds > numClubs:
points = numDiamonds
print (currentPlayer, "has", points, "points")
elif numClubs > numHearts and numClubs > numSpades and numClubs > numDiamonds:
points = numClubs
print (currentPlayer, "has", points, "points")

return points
return splitValue, splitSuit

#printing cards from deck as discard pile

def firstHand(cardHand):
player1 = bothHands[:1]
print ("first half", player1)
return player1

def secondHand(cardHand):
player2 = bothHands[1:]
print ("second half", player2)
return player2

#MAIN PROGRAM -------------------------------------------
name1 = input("Player 1 name: ")
name2 = input("Player 2 name: ")
print("Welcome", name1, "and", name2, "! Let's play 31")

currentPlayer = name1

for count in range(2):
deck = make_deck()
creating_hand = make_hand(deck)
cardHand = hands(deck)
usedCards = used_cards(deck, cardHand)
scoreOfHand = score_hand(cardHand)

#toggling players
if currentPlayer == name1:
currentPlayer = name2
else:
currentPlayer = name1

while len(deck) > 0:

hand1 = firstHand(cardHand)
hand2 = secondHand(cardHand)
currentPlayer = hand1
current_Player = name1

#if player has 31 points or around 31 points, player can knock to end game
knockToEndGame = input("Would you like to knock to end game? (Y/N) ").upper()
while not knockToEndGame.isalpha() and knockToEndGame not in "YN":
knockToEndGame = input("Invalid option. Would you like to knock to end game? (Y/N) ").upper()
if knockToEndGame == "Y":
print (current_Player, "wins")
break
else:
print ("continuing...")

#from the rest of the deck, players can replace the top card with a card from their hand to add to their pile creating a higher score
takeCard = input("Would you like to take from the discard pile? (Y/N) ").upper()
while not takeCard.isalpha() and takeCard not in "YN":
takeCard = input("Invalid option. Would you like to take from the discard pile? (Y/N) ").upper()
if takeCard == "Y":
delCard = input("Which card would you like to remove? (Ex: 2 ♥) ")
if delCard in currentPlayer:
currentPlayer.remove(delCard)
print(currentPlayer)
elif delCard not in currentPlayer:
print (currentPlayer)

#more toggling of players
if currentPlayer == hand1:
currentPlayer = hand2
else:
currentPlayer = hand1

if current_Player == name1:
current_Player = name2
else:
current_Player = name1``````

#2

We played this game alot in Sea Cadets, especially on excursions. Great game. Need a refresher, though. Can you give us a brief run down, please. Thanks.

10 or face cards are 10, Ace is 11, three of a kind is 30-and-a-half? Three quarters and a rubber?

I'll see what I can glean from the code in the meantime.

The second return is unreachable.

#3

every player (in this case 2) gets 3 cards from the deck. the objective is to get the closest score (sum of 3 cards) to 31. Ace is 11 and 10/ face cards are worth 10 points (for me, i've chose not to do the three of a kind). Players can only add numbers from the same suit. For example: if my hand was 5 clubs, 2 hearts and 10 clubs, i could only add the 5 and 10 of clubs for a score of 15. once the players receive their hands, they can either "knock" to end the game or pick up a card from the discard pile. the discard pile is the remaining cards from the deck. The discard pile is a way for players to increase their score by replacing one card with the discard card. Once that player has either knocked or picked a card, then it's the next players turn to either knock or choose a card.

thank you so much

#4

Seems we played with three quarters, dimes, nickels, whatever we could afford, and once you lost your three coins, you had one honor chance, which as long as you had it, you stayed in the game. The last man standing was the winner. Usually the game is better played between 3 to 6 players, but it still comes down to the final two, so a two person game is perfectly feasible, though boring in short order.

#5

haha that sounds quite interesting. I changed my code to only return 1 thing. The big problem:

from the rest of the deck, players can replace the top card with a card from their hand to add to their pile creating a higher score

takeCard = input("Would you like to take from the discard pile? (Y/N) ").upper()
while not takeCard.isalpha() and takeCard not in "YN":
takeCard = input("Invalid option. Would you like to take from the discard pile? (Y/N) ").upper()
if takeCard == "Y":
delCard = input("Which card would you like to remove? (Ex: 2 ) ")
if delCard in currentPlayer:
currentPlayer.remove(delCard)
print(currentPlayer)

Essentially, delcard should be in currentPlayer, but it says it isnt. Do you know the solution to that problem?

#6

The two choices are draw or glean, respectively from the deck or the discards. Give the player one of two choices. The action is immediate and the appropriate card goes into the hand.

The discard will be from the four cards they hold.

What this cries for is an input subroutine that is custom made for the game. Give it choices and it cannot return anything but.

Edit: three choices... knock.

#7

I feel like I tried with delcard and I don't know how else I could approach this

#8

If I took out the "if delcard in player" do you think that would work?

#9

Don't try to fix your code. Write new code that will replace it (with slightly different names). Look for reusability and multifunctional approaches to limit the verbosity and nail down the logic and states. If it means going back to the drawing board, then so be it. Don't throw away the baby. Just have another one.

#10

I kinda have a deadline for this which is Friday. I don't know if I can start from scratch

#11

You don't have to. Just get to work on that input subroutine to handle all inputs. Then your code will shrink down. Pencil this one out on paper and route everything through the input subroutine.

``````param1 => prompt or prompt # if you create a list or dictionary to handle this stuff
param2 => accepted returns which can also be supplied by dictionary``````

The optional third parameter would be a callback that is given the returns. We can handle that inline, though so optional.

Edit:

``````prompts = [
{}, {}, {}
]``````

#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.