我不知道如何循环我的代码的特定部分

Posted

技术标签:

【中文标题】我不知道如何循环我的代码的特定部分【英文标题】:I dont know how to go about looping a specific part of my code 【发布时间】:2022-01-12 21:43:48 【问题描述】:

我正在编写一个基于赌场的游戏,但在编写二十一点时遇到了一些问题,我遇到了一个问题,您只能选择“击中”一次,而且我不知道如何实现环形。一旦你“击中”了它,它就会像你的分数一样解决,即使你可能还远低于 21 岁。每次我尝试修复它时,代码的其他部分就会中断。

(请记住,这不是完整的代码,而只是二十一点的部分)

import os
import random

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
bal = 100
balstr = str(bal) + "$"

def clear():
    os.system('cls')

def deal(deck):
    hand = []
    for i in range(2):
        random.shuffle(deck)
        card = deck.pop()
        if card == 11:card = "J"
        if card == 12:card = "Q"
        if card == 13:card = "K"
        if card == 14:card = "A"
        hand.append(card)
    return hand

def newRound():
    again = input("Do you want to play again? (Y/N): ").lower()
    if again == "y":
        blackjack()
    else:
        #takes you back to main menu in the full code, just ignore this
        position()

def total(hand):
    total = 0
    for card in hand:
        if card == "J" or card == "Q" or card == "K":
            total+= 10
        elif card == "A":
            if total >= 11: 
                total+= 1
            else: total+= 11
        else:
            total += card
    return total

def hit(hand):
    card = deck.pop()
    if card == 11:
        card = "J"
    if card == 12:
        card = "Q"
    if card == 13:
        card = "K"
    if card == 14:
        card = "A"
    hand.append(card)
    return hand

def currentHands(dealerHand, playerHand):
    clear()
    print(("The dealer has a ") + str(dealerHand) + " for a total of " + str(total(dealerHand)))
    print(("You have a ") + str(playerHand) + " for a total of " + str(total(playerHand)))

def score(dealerHand, playerHand, usrbetint):
    global bal

    if total(playerHand) == 21 or total(dealerHand) > 21 or total(playerHand) > total(dealerHand) and total(playerHand) < 21:
        currentHands(dealerHand, playerHand)
        bal += usrbetint
        print("Congratulations, you win!\n \nYour new balance is $".format(bal))
    else :
        currentHands(dealerHand, playerHand)
        bal -= usrbetint        
        print("Sorry, you lose.\n \nYour new balance is $".format(bal))

    
def blackjack():
    choice = 0
    clear()
    print("Let's play blackjack!\n")
    userbet = input("(for help type help) How much money do you want to use: ").upper()
    if userbet == "HELP" :
        if userbet == "HELP" :
            print("Instructions")
        else :
            print("Something went wrong")
            pass
    else :
        usrbetint = int(userbet)
    dealerHand = deal(deck)
    dealerHandShow = [dealerHand[0]]
    dealerHandShow = total(dealerHandShow)
    playerHand = deal(deck)
    print(("The dealer is showing a ") + str(dealerHand[0]) + " for a total of " + str(dealerHandShow))
    print(("You have a ") + str(playerHand) + " for a total of " + str(total(playerHand)))
    choice = input("Do you want to [H]it or [S]tand?: ").lower()
    clear()
    if choice == "h":
        hit(playerHand)
        while total(dealerHand) < 17:
            hit(dealerHand)
        score(dealerHand, playerHand, usrbetint)
        newRound()
    elif choice == "s":
        while total(dealerHand) < 17:
            hit(dealerHand)
        score(dealerHand, playerHand, usrbetint)
        newRound()

blackjack()

我认为修复将在“二十一点”函数的最后 20 行左右,但不知道如何在不发送代码块的情况下解释所有内容。

如果有人能给我一些关于在哪里更改内容的提示,我将非常感激并忽略“全局 bal”部分,这是我知道添加真正全局变量的唯一方法。

【问题讨论】:

【参考方案1】:

由于您不知道要循环多少次(它基于用户输入),您可能需要一个while 循环。您当前的代码将经销商行为混合到玩家处理中,您需要将其分开,因为我们不想循环它。

print("You have a " + str(playerHand) + " for a total of " + str(total(playerHand)))
choice = input("Do you want to [H]it or [S]tand?: ").lower()
while choice == "h":
    clear()
    hit(playerHand)
    print("You have a " + str(playerHand) + " for a total of " + str(total(playerHand)))
    choice = input("Do you want to [H]it or [S]tand?: ").lower()

while total(dealerHand) < 17:
    hit(dealerHand)

score(dealerHand, playerHand, usrbetint)

您可能需要添加一个附加条件,以停止询问玩家是否想在他们已经出局时击球。

【讨论】:

你是个传奇人物

以上是关于我不知道如何循环我的代码的特定部分的主要内容,如果未能解决你的问题,请参考以下文章

中断for循环后如何重置指针

继续后如何返回循环的特定部分?

通过数据框进行条件循环

如何找出代码中存在哪些特定的循环引用

为什么BlueJ给我的代码一个“无法访问的语句”错误? [重复]

错误的构造或循环崩溃?