Python 21点循环

Python 21点循环,python,blackjack,Python,Blackjack,我的21点游戏有效,但我如何循环它,以便在游戏结束时,他们可以选择再次玩?谢谢大家!只需要一个循环。干杯 import random endGame = (False) dealer = random.randrange(2,20) player = random.randrange(2,20) print ("\n*********LETS PLAY BLACK JACK***********\n") print ("Your starting total is "+str(player

我的21点游戏有效,但我如何循环它,以便在游戏结束时,他们可以选择再次玩?谢谢大家!只需要一个循环。干杯

import random
endGame = (False)


dealer = random.randrange(2,20)
player = random.randrange(2,20)

print ("\n*********LETS PLAY BLACK JACK***********\n")
print ("Your starting total is "+str(player))



while endGame==(False):
    action =input("What do you want to do? stick[s] or twist[t]? ")
    if action == ("s"):
        print ("Your total is "+str(player))
        print ("Dealer's total is "+str(dealer))
        if player > dealer:
            print ("*You win!*")
        else:
            print ("Dealer wins")
            endGame = True

    if action == ("t"):
        newCard = random.randrange(1,10)
        print ("You drew "+str(newCard))
        player = player + newCard
        if player > 21:
            print ("*Bust! You lose*")
            endGame = True
        else:
            print ("Your total is now "+str(player))
            if dealer < 17:
                newDealer = random.randrange(1,10)
                dealer = dealer + newDealer

                if dealer > 21:
                    print ("*Dealer has bust! You win!")
                    endGame = True
随机导入
终局=(假)
经销商=随机。随机范围(2,20)
玩家=随机。随机范围(2,20)
打印(“\n**********让我们玩黑杰克游戏************\n”)
打印(“您的起始总数为”+str(玩家))
而endGame==(False):
动作=输入(“您想做什么?粘贴[s]或扭转[t]?”)
如果操作==(“s”):
打印(“您的总数为”+str(玩家))
打印(“经销商总额为”+str(经销商))
如果玩家>经销商:
打印(“*您赢了!*”)
其他:
打印(“经销商获胜”)
终局
如果操作==(“t”):
newCard=random.randrange(1,10)
打印(“你画的”+str(新卡))
玩家=玩家+新卡
如果玩家>21:
打印(“*半身像!你输了*”)
终局
其他:
打印(“您的总数现在是”+str(玩家))
如果经销商<17:
newDealer=random.randrange(1,10)
经销商=经销商+新经销商
如果经销商>21:
打印(“*经销商破产!您赢了!”)
终局

您可以在循环时将其包装在另一个
中,或者有两个单独的函数

while True:
    endgame = False
    while not endgame:
        #game actions
    play_again = raw_input("Play again y or n?")
    if play_again == 'n':
        break
或者甚至将其分为不同的函数:

def play_again():
    play_option = raw_input("Play again y or n?")
    if play_option == 'y': game_play()


def game_play():
    endgame = False
    while not endgame:
        #game_actions
    play_again()

通过在循环末尾添加以下行,您的问题应该得到解决

if (endGame) :
  print ("Do you want to play again? Press 1") #and take then change endGame to false
  #get the input
  #confirm it is the value you want
  if(input == 1):
     endGame=False
     #reset the values for dealer and player
     #clear screen, print the infos
  else:
     print("Thank you for playing black jack!")

非常感谢。我只是把整个程序包装在一个额外的循环中,我应该想到这一点!谢谢你的其他建议。谢谢你的建议,先生