【问题标题】:Structure and logic issues on my Blackjack game Python code我的二十一点游戏 Python 代码的结构和逻辑问题
【发布时间】:2020-03-06 11:48:20
【问题描述】:

我正在为学校作业编写二十一点游戏,我试图了解结构/逻辑的某些部分。

目前,我在为闲家/庄家手牌汇总随机生成的牌时遇到问题。此外,我在结构方面遇到问题,并在 main() 中调用我的函数。

import random
Dealer_Chips = 500
Player_Chips = 500
Pot = 0

deck = {'Two': 2, 'Three' : 3, 'Four' : 4, 'Five' : 5, 'Six' : 6, 'Seven' : 7, 'Eight' : 8, 'Nine' : 9, 'Jack' : 10, 'Queen' :10, 'King' : 10, 'Ace' : 11}

Player_Hand = [random.choice(list(deck)), random.choice(list(deck))] #Creates Player hand
Dealer_Hand = [random.choice(list(deck)), random.choice(list(deck))] #Creats Dealer Hand
Player_Score = 0
Dealer_Score = 0

    enter code here

def Ace():
    if Player_Hand == 'Ace':
        input("You drew an Ace, Please choose whether the Ace is a 1 or 11")



def Hit():
    Player_Hand + random.choice(list(deck))
    print("You chose to Hit, Your hand is", Player_Hand)


def Stay():
    print ("stay")


def Bet():
    input("Please place a bet. Bets can be either 5, 10, 25, 50")
if input == 5:
        Pot + 5
        if input == 10:
            Pot + 10
            if input == 25:
                Pot + 25
                if input == 50:
                    Pot + 50

def Winner():
    print("Congratulations, you win!")
    Player_Chips + Pot

def Main():
    print("Welcome to Blackjack!")
    print("Your Hand is", Player_Hand, "Your Chip Count is", Player_Chips)
    input == Bet()
    input("Would you like to hit or stay? Enter H to hit, or S to stay")
    if input == "H":
        Hit()
    if input == "S":
        Stay()





Main()

【问题讨论】:

  • 您能否提供您正在尝试做的事情以及出了什么问题?您的代码的一些输出也将有助于调试
  • 很抱歉没有添加输出。这是正在发生的事情。 # 欢迎来到二十一点!您的手牌是 ['Seven', 'Ace'] 您的筹码数是 500 请下注。赌注可以是 5、10、25、50 你想打还是留下?输入 H 击中,或输入 S 停留。我输入H,游戏结束。进程以退出代码 0 结束

标签: python python-3.x list pygame blackjack


【解决方案1】:

在这段代码上还有很多工作要做,所以我会记录一些对我来说很突出的事情。

def Bet():
    input("Please place a bet. Bets can be either 5, 10, 25, 50")
if input == 5:
        Pot + 5
        if input == 10:
            Pot + 10
            if input == 25:
                Pot + 25
                if input == 50:
                    Pot + 50
  1. 不确定这是否只是一个复制+粘贴错误,但是这方面的缩进已经全部关闭——您的ifs 链从您的函数定义之外开始,因此它们不会作为函数。
  2. 您的if 语句相互嵌套,这意味着内部语句永远不会发生(因为input 不可能同时出现,例如5 和10)。
  3. 无论如何它们都不会发生,因为您input 是一个字符串并且正在与整数进行比较。
  4. 您不是在查看 input 函数返回的值,而是在与 input 函数本身进行比较。

此函数的固定版本可能如下所示:

def Bet():
    # Get the bet from the user by inputting a string and converting it to an int.
    bet = int(input("Please place a bet. Bets can be either 5, 10, 25, 50: "))
    # Make sure that the bet is within the set of allowed values.
    assert(bet in [5, 10, 25, 50])
    # Add it to the Pot global variable.
    Pot += bet

现在,看看Main() 函数:

def Main():
    print("Welcome to Blackjack!")
    print("Your Hand is", Player_Hand, "Your Chip Count is", Player_Chips)
    input == Bet()  # see notes 1, 2, and 3
    input("Would you like to hit or stay? Enter H to hit, or S to stay") # 4
    if input == "H":
        Hit()  # 5
    if input == "S":
        Stay()
  1. Bet() 不返回任何值,因此没有理由为其返回赋值。
  2. input 函数分配一些东西会使该函数无法使用。
  3. 等式运算符== 不适用于赋值。
  4. 同样,您需要为input 的返回值赋值,例如action = input("...")
  5. 一旦你已经Hit()Stay()ed,你需要做更多的事情来更新游戏的状态。如果你Hit()你想返回并再次提示用户,如果你Stay()那么你想把分数加起来。

我认为您在这方面需要的帮助比单个 stackoverflow 答案所能提供的更多,因此我建议您与您的老师交谈,并请他们指导他们希望您如何完成作业 - 例如,他们是否解释过循环给你了吗?

祝你好运! :)

【讨论】:

  • 感谢您的回复。我确实理解投注功能中的缩进非常不稳定且写得不好。出于某种原因,我认为 IF 决定必须缩进。我将重写这部分。我同意我确实需要更多的帮助和练习,编程从来都不是我最擅长的技能。我将重新访问并添加到 Hit() 和 Stay() 部分以保持游戏继续进行。这实际上是一个主要问题,因为我的套牌没有比较玩家/庄家手牌的价值,因此很难编写游戏的下一部分。所有这些建议都非常有用。
  • 也感谢您向我展示“断言”,我以前从未见过这种方法,它看起来像是在允许的投注范围内验证输入的更好方法。
  • if 语句确实需要缩进,但是你把缩进放在哪里很重要;您不能只是随意点击 Tab 并期望您的程序执行您想要的操作。 :) 你在if 下缩进的任何事情都只会发生在if 谓词是真的——所以如果你在if 内嵌套一个if,那么那个里面的事情只会发生如果 both 谓词为真(如果这两个谓词是 n == 5n == 10 它们不能两者同时为真,对吧?)。
【解决方案2】:

我们开始吧:)

input("Would you like to hit or stay? Enter H to hit, or S to stay")

input 是一个内置函数。您将它与变量混淆了。您需要创建一个具有不同名称的变量(我将使用Input_Variable 作为此答案的其余部分),并且您需要将input() 的结果存储在所述变量中。像这样:

InputVariable = input("Would you like to hit or stay? Enter H to hit, or S to stay")
input == Bet()

Input_Variable == xInput_Variable = x 不是同一个东西。 Input_Variable == x 检查如果Input_Variable 等于x,并且Input_Variable = x 使 Input_Variable 等于 x。

def Bet():
    InputVariable = input("Please place a bet. Bets can be either 5, 10, 25, 50")
if InputVariable == 5:

在 Python 中,您需要对行进行缩进以使其成为语句的一部分。您的缩进也需要保持一致:

def Bet():
    input("Please place a bet. Bets can be either 5, 10, 25, 50")
    if input == 5: # this needs to be indented by four spaces or one tab
        # more code goes here, indented by eight spaces or two tabs
if input == 5:
    Pot + 5
    if input == 10:
        Pot + 10
        if input == 25:
            Pot + 25
            if input == 50:
                Pot + 50

Pot + 5不给Pot加5,然后把结果存入Pot:如果你想给Pot加5并将结果存入Pot,你需要像@987654337这样@。

此外,以这种方式做事没有意义。做吧

Pot = Pot + InputValue
Player_Hand = [random.choice(list(deck)), random.choice(list(deck))]

我不确定您要在这里做什么,但这似乎不对。请澄清。

我可能遗漏了一些事情,但希望这会对您有所帮助。祝你的学校作业好运!

【讨论】:

  • 非常感谢您的回复。将玩家的 Bet 输入设置为一个变量,然后将该变量添加到底池中,这样会更有意义。至于这部分# Player_Hand = [random.choice(list(deck)), random.choice(list(deck))] #,我试图从牌堆中随机抽取两张随机牌放入玩家和庄家手中.完成此操作后,我会将两张牌加在一起,让我能够比较并确定谁是二十一点。最后,感谢您解释 = 和 == 之间的区别,这澄清了我收到的错误。
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 2011-02-04
  • 2015-08-02
  • 2016-06-21
  • 2011-06-30
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多