Python 初学者很难看出我的错误在哪里:(

问题描述 投票:0回答:1

在我的 Python 之旅中,我是一个完全的初学者,我通过视频和几个课程进行学习,我完成了我的第一个顶点项目 - 简单的二十一点,我决定通力合作,不使用任何关于我的代码结构的帮助,这就是我的结果最终,但我的代码中似乎有一些例外,我很难看到,图像显示代码进入某种循环,对马虎代码表示歉意:(在此处输入图像描述

import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
game_start = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ")

player_cards = []
player_score = 0
computer_cards = []
computer_score = 0
ace_count = 0


def ace():
    if 11 in player_cards:
        global ace_count
        global player_score
        ace_count += 1
        if player_score > 21 and ace_count != 0:
            player_score -= 10


def player_deal():
    deal = random.choice(cards)
    player_cards.append(deal)
    global player_score
    player_score += deal
    ace()
    return


def computer_deal():
    deal = random.choice(cards)
    computer_cards.append(deal)
    global computer_score
    computer_score += deal
    ace()
    return


def score():
    print(f"Player cards are [{player_cards}], and Player score is {player_score}")
    print(f"Computer cards are [{computer_cards}], and Computer score is [{computer_score}]")


if game_start == 'y':
    player_deal()
    player_deal()
    ace()
    print(f"Player cards are [{player_cards}], and Player score is {player_score}")
    computer_deal()
    computer_deal()
    ace()
    print(f"Computer first card is [{computer_cards[0]}]")
    if computer_score == 21:
        score()
        print("Computer wins!")
    elif player_score == 21:
        score()
        print("Player wins!")
    else:
        while player_score < 21 or computer_score < 21:
            another_card = input("Pres 'y' to get another card, Press 'n' to pass: ")
            if another_card == 'y':
                player_deal()
                if player_score == 21:
                    score()
                    print("Player Wins!")
                    break
                elif player_score > 21:
                    score()
                    print("Computer Wins!")
                    break
            else:
                if computer_score < 17:
                    computer_deal()
                    if computer_score > 21:
                        score()
                        print("Player wins!")
                        break
                    elif player_score > computer_score:
                        score()
                        print("Player wins!")
                        break
                    elif player_score < computer_score:
                        score()
                        print("Computer wins!")
                        break
                    else:
                        score()
                        print("It's a draw!")
                        break
else:
    print("Have a nice day!")

按“y”继续并抽更多牌后,我的while循环似乎进入无限循环,有时当我按“n”时也会发生

python-3.x
1个回答
0
投票

根据你的while循环

while player_score < 21 or computer_score < 21:
,这意味着只要玩家的分数或计算机的分数小于21,循环就会继续。

但是,在循环内,您不会更新分数。当玩家选择通过时,您不会更新计算机的分数,直到计算机的分数达到 17 或更高。这意味着如果计算机的分数低于17,它将继续抽牌,导致无限循环。

您可以更新此:-

if game_start == 'y':
    player_deal()
    player_deal()
    ace()
    print(f"Player cards are [{player_cards}], and Player score is {player_score}")
    computer_deal()
    computer_deal()
    ace()
    print(f"Computer first card is [{computer_cards[0]}]")
    if computer_score == 21:
        score()
        print("Computer wins!")
    elif player_score == 21:
        score()
        print("Player wins!")
    else:
        while True:
            another_card = input("Press 'y' to get another card, Press 'n' to pass: ")
            if another_card == 'y':
                player_deal()
                if player_score > 21:
                    score()
                    print("Computer wins!")
                    break
                elif player_score == 21:
                    score()
                    print("Player wins!")
                    break
            else:
                while computer_score < 17:
                    computer_deal()
                    if computer_score > 21:
                        score()
                        print("Player wins!")
                        break
                    elif computer_score == 21:
                        score()
                        print("Computer wins!")
                        break
                score()
                if player_score > computer_score:
                    print("Player wins!")
                elif player_score < computer_score:
                    print("Computer wins!")
                else:
                    print("It's a draw!")
                break
else:
    print("Have a nice day!")
© www.soinside.com 2019 - 2024. All rights reserved.