简体   繁体   中英

Python Function code works fine but when defined into a function does not work

I am currently taking a Udemy course for Python and am on the Functions part of the lesson. I am attempting to create a Black Jack game using functions.

I created a function to prevent duplicate cards from being drawn by comparing them to a list of already drawn cards. If the card drawn is in the list the function calls for a new card to be drawn and again checked against the list. If it is not in the list the function appends the card to the drawn list (used_cards) to be checked further down the code.

The issue I have is that the code actually contained in the function works as intended when placed in the code, however when I define the same code in a function it is not changing the value of a card that is already in the drawn cards list as intended. dupcheck is the name of the function I am referring too. Thank you for any help as I am at a brick wall.

import random

cards = {}
cards[1] = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'King', 'Queen', 'Ace']
cards[2] = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'King', 'Queen', 'Ace']
cards[3] = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'King', 'Queen', 'Ace']
cards[4] = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'King', 'Queen', 'Ace']

face_cards = {'Jack': 10, 'King': 10, 'Queen': 10, 'Ace': 1, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}

card_class = {1: 'Hearts',
              2: 'Clubs',
              3: 'Spades',
              4: 'Diamonds'}


# Draws a random card from the deck
def rndm_card():
    class_string = random.randint(1, 4)
    rndmcard = random.randint(0, 12)
    card_name = cards[class_string][rndmcard], 'of', card_class[class_string]
    cardvalue = face_cards[cards[class_string][rndmcard]]

    return (card_name)


# Returns the value for a players card
def card_vlu(card):
    return face_cards[card[0]]


# Prints a string about a cards contents
def cardstring(card):
    print(card[0], card[1], card[2])


# Creates sum of hands
def cardsum(card1, card2):
    return (card_vlu(card1) + card_vlu(card2))


# Determines if an Ace is hard or soft
def acecheck(card1, card2):
    if card_vlu(card1) + card_vlu(card2) < 12:
        face_cards['Ace'] = 11


# Adds card to used_list and makes sure no card can be drawn that has already been drawn.
def dupcheck(card):
    while card in used_cards:
        card = list(rndm_card())
        # I have tried 'return card' here with no luck
    while card not in used_cards:
        used_cards.append(card)

used_cards = []
print(used_cards)

input('Welcome to BlackJack! Press Enter to deal: ')
#Players hand
print('Your cards are: ')
player_1card1 = [6, 'of', 'Clubs']

while player_1card1 in used_cards:
    player_1card1 = list(rndm_card())

while player_1card1 not in used_cards:
    used_cards.append(player_1card1)

player_1card2 = [6, 'of', 'Clubs']
dupcheck(player_1card2)                #Code in function identical to code below yet does not work


# while player_1card2 in used_cards:   #When these five lines are un commented out the code works.        
#     player_1card2 = list(rndm_card())
#
# while player_1card2 not in used_cards:
#     used_cards.append(player_1card2)
acecheck(player_1card1,player_1card2)
print(used_cards)
cardstring(player_1card1)
cardstring(player_1card2)
print(cardsum(player_1card1,player_1card2))

however when I define the same code in a function it is not changing the value of a card that is already in the drawn cards list as intended.

It took me a long time to figure out what you were actually describing - ie what you expect to happen, and how it's different from what actually happens.

player_1card2 = [6, 'of', 'Clubs']
dupcheck(player_1card2)

This does not change the contents of player_1card2 outside the function. Inside the function,

def dupcheck(card):
    while card in used_cards:
        card = list(rndm_card())
        # I have tried 'return card' here with no luck
    while card not in used_cards:
        used_cards.append(card)

card = list(rndm_card()) creates a new value, and makes the local variable card be a name for that new value. It does not affect the passed-in list.

I have tried 'return card' here with no luck

This is only half of the equation. You also need to use the returned value, for example player_1card2 = dupcheck(player_1card2) .


But really, the entire general approach is strange, in multiple ways. You don't choose a card from a deck by taking the six of clubs arbitrarily and then changing the selection until you have something that wasn't already picked. You choose a card from a deck by putting everything that can be picked into the deck, and then picking one out (you can even random.shuffle the deck and then take them off the end, rather than making a random.choice from the middle each time). Also, you clearly already know about lists, so why isn't the player's hand a list?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM