21点游戏Python代码的结构和逻辑问题

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

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

[目前,我在获取随机生成的卡来对付玩家/交易者手牌时遇到问题。此外,我在结构上存在问题,并在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()
python python-3.x list pygame blackjack
2个回答
1
投票

这里,我们去:)

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

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

input == Bet()

Input_Variable == xInput_Variable = x是不同的东西。Input_Variable == xchecks如果Input_Variable等于x,并且Input_Variable = x makes Input_Variable等于x。

InputVariable = input("Would you like to hit or stay? Enter H to hit, or S to stay")
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不会将5加到Pot中,然后将结果存储在Pot中:如果要将5加到Pot中并将结果存储到Pot中,则需要执行类似Pot = Pot + 5的操作。

而且,以这种方式做事没有意义。刚做

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

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

我可能错过了几件事,但是希望这会对您有所帮助。祝您学校作业好运!


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. [不确定这是否只是复制+粘贴错误,但是缩进全部关闭-if链从函数定义之外开始,因此它们不会在函数定义中发生功能。
  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()完成后,您需要做更多的工作来更新游戏状态。如果您选择Hit(),请返回并再次提示用户,如果您选择Stay(),则要对分数进行累加。

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

祝你好运! :)

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