首页 > 解决方案 > 看不到更新变量

问题描述

我正在开发一个控制台二十一点游戏,我无法检查是否是玩家轮流。它以前工作过,但突然间我看不到让它工作

这是控制是否轮到玩家的代码:

#first I declare the variable (p for the players turn and d for the dealer)
current_turn = "p"
#then i loop through the choice function until the current_turn is not equal to "p"
while current_turn == "p":
    choice()
#this is how that function looks
def choice():
    if player_bust() == True:
        print("YOU BUSTED PLEASE TRY AGAIN")
        exit()
    turn_choice = input(
        "What would you like to do (h for Hit, s for Stay): ")
    if turn_choice == "h":
        hit()
    elif turn_choice == "s":
        print("Passing turn to dealer")
        current_turn = "d"
    else:
        print("INVALID CHOISE, PLEASE TRY AGAIN")
        exit()
#NOTE THAT THE ELIF STATEMENT WHERE THE TURN CHOISE IS s. current_turn is greyed out when assigning it

完整代码:

import random

def PlayGame(numdecks):

current_turn = "p"

def new_deck():
    std_deck = [
        # 2  3  4  5  6  7  8  9  10  J   Q   K   A
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
    ]

    # add more decks
    std_deck = std_deck * numdecks

    random.shuffle(std_deck)

    return std_deck[:]

deck = new_deck()

player_cards = []
dealer_cards = []

def setup():
    player_cards.append(deck.pop(0))
    dealer_cards.append(deck.pop(0))
    player_cards.append(deck.pop(0))
    dealer_cards.append(deck.pop(0))

    if sum(player_cards) > 21:
        player_bust()
    print(f"Visable dealer card: {dealer_cards[1]}")
    print(f"Your cards : {sum(player_cards)}")
    if sum(player_cards) == 21:
        print("CONGRATS YOU WON!")
        exit

def choice():
    if player_bust() == True:
        print("YOU BUSTED PLEASE TRY AGAIN")
        exit()
    turn_choice = input(
        "What would you like to do (h for Hit, s for Stay): ")
    if turn_choice == "h":
        hit()
    elif turn_choice == "s":
        print("Passing turn to dealer")
        current_turn = "d"
    else:
        print("INVALID CHOISE, PLEASE TRY AGAIN")
        exit()

def show_card():
    print(f"Dealer cards: {sum(dealer_cards)}")
    print(f"Your cards: {sum(player_cards)}")

def hit():
    print(f"You picked up {deck[0]}")
    player_cards.append(deck.pop(0))
    player_bust()
    show_card()
    choice()

def player_bust():
    if sum(player_cards) > 21:
        for index, card in enumerate(player_cards, start=0):
            if card == 11:
                player_cards[index] = 1
                if sum(player_cards) > 21 and 11 in player_cards:
                    player_bust()
        if sum(player_cards) > 21:
            return True
    return False

setup()
while current_turn == "p":
    choice()

上面的所有代码都在 PlayGame 函数中,如果代码看起来很乱,我也很抱歉

标签: pythonfunction

解决方案


推荐阅读