首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python blackjack循环

Python blackjack循环
EN

Stack Overflow用户
提问于 2013-11-11 14:51:30
回答 2查看 1.9K关注 0票数 0

我的Blackjack游戏可以工作,但我怎么能循环它,这样在游戏结束时,他们可以选择再玩一次?多谢各位!只需要循环。干杯

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-11 14:54:48

您可以将它包装在另一个while循环中,或者有两个独立的函数。

代码语言:javascript
复制
while True:
    endgame = False
    while not endgame:
        #game actions
    play_again = raw_input("Play again y or n?")
    if play_again == 'n':
        break

甚至把它分成不同的函数:

代码语言:javascript
复制
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()
票数 0
EN

Stack Overflow用户

发布于 2013-11-11 14:56:58

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

代码语言:javascript
复制
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!")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19909189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档