Python - 只要列表的总和小于 17,我就会添加到列表。while 循环有效,除非总和在 12 和 16 之间。我不知道为什么

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

我正在编写 Black Jack 应用程序的简单版本。一切正常,我检查计算机/经销商是否有<=16. Here is an image of it working:

在此输入图片描述

一旦用户/玩家完成游戏,While 循环将检查计算机 SUM 是否为 <17, then deals continously until Computer sum is higher; before finally checking the winning conditions. I noticed that it all works except for when computer sum is between 12 & 16.It just haults.

在此输入图片描述

我知道有几种方法可以做到这一点,我自己写了这个,希望有人可以看看并告诉我我做错了什么。

我是新手,所以请原谅您发现的任何新手或不干净的代码。我希望我提供了足够的细节。 谢谢。

这是我的代码:

import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

def black_jack():
    user = []
    computer = []

    def deal_card():
        """Deals a random card/number"""
        deal = random.choice(cards)
        return deal

    def ace_check():
        """This is checking list of cards for user and Computer
        --- Swaps 11 for 1 if sum is greater than 21"""
        if sum(user) > 21:
            for x in range(len(user)):
                if user[x] == 11:
                    user[x] = 1
        if sum(computer) > 21:
            for y in range(len(computer)):
                if computer[y] == 11:
                    computer[y] = 1

    def mask_hand():
        """Replaces the last item of the list with X...
        using a copy of the original list"""
        masked = computer.copy()
        # learnt about the .copy which creates a copy of list.
        masked[len(masked) - 1] = "X"
        return masked

    def winning_conditions():
        ace_check()
        if sum(computer) < sum(user) <= 21:
            winner = "User"
            print(f"user{user}={sum(user)}, computer{computer}={sum(computer)} - {winner} Wins!!!")
        if sum(user) < sum(computer) <= 21:
            winner = "Computer3"
            print(f"user{user}={sum(user)}, computer{computer}={sum(computer)} - {winner} Wins!!!")
        if sum(user) == sum(computer):
            print(f"user{user}={sum(user)}, computer{computer}={sum(computer)} - It's a DRAW!!!")
        if sum(computer) == 21 and sum(user) <= 21:
            winner = "Computer2"
            print(f"user{user}={sum(user)}, computer{computer}={sum(computer)} - {winner} Wins!!!")
        if sum(user) > 21:
            winner = "Computer1"
            print(f"user{user}={sum(user)}, computer{computer}={sum(computer)} - {winner} Wins!!!")
            return

    def comp_sum_check():
        while sum(computer) <= 16:
            computer.append(deal_card())
            return computer

    welcome = input(f"Welcome, are you ready to deal? y or n:\n")
    if welcome == "n":
        bye_message = "Bye!!!"
        return print(bye_message)
    elif welcome == "y":
        user.append(deal_card())
        user.append(deal_card())
        computer.append(deal_card())
        computer.append(deal_card())
        print(f"user{user}, computer{computer}")

        while sum(user) < 22:
            ask_deal = input(f"Would you like to deal? y or n:\n")
            if ask_deal == "n":
                comp_sum_check()
                winning_conditions()
                return
            elif ask_deal == "y":
                user.append(deal_card())
                ace_check()
                print(f"user{user}, computer{mask_hand()}")
        else:
            ace_check()
            winning_conditions()


black_jack()

while-loop boolean blackjack
1个回答
0
投票

问题是您有一个

return
语句在此循环的第一次迭代中执行:

    def comp_sum_check():
        while sum(computer) <= 16:
            computer.append(deal_card())
            return computer

因此,循环永远不会进行第二次迭代。

return
语句移出循环体:

    def comp_sum_check():
        while sum(computer) <= 16:
            computer.append(deal_card())
        return computer

这解决了您提到的问题。还有其他问题,例如如何将 A 的值替换为 1:如果玩家有两张 A,这将无法正确完成工作。在这种情况下,您不应该替换它们的两个值,而只替换其中一个,然后继续,直到可能再次出现 22 或更多的多余值,然后才将另一个 ace 替换为 1。

© www.soinside.com 2019 - 2024. All rights reserved.