线上游戏无效。为什么不能正确显示赢得和输掉的游戏?

时间:2017-11-30 18:13:08

标签: python python-3.x

这是我21或21日游戏的代码。在最后一行,它不会显示赢得的游戏数量和失去的游戏数量,但我无法弄清楚如何解决它。有人可以帮我纠正这个吗?我在python 3.6.1

上做了这个

P.S。如果还有其他任何可以改进的地方,那么反馈将非常受欢迎。

import random
from random import choice
import time
import colorama
from colorama import Fore, Back, Style
name = str(input("Welcome to " + Fore.YELLOW + "21" + Style.RESET_ALL + "! I'll be your dealer. \nWhat's your name?"))
instructions=""
while instructions!="N" and instructions!="Y":
  instructions=str(input("Would you like to see the Instructions? Y/N"))
  if instructions!="N" and instructions!="Y":
    print("That is not a valid answer.")
  else:
    time.sleep(1)
if instructions=="Y":
  print(Fore.GREEN + "Instructions:" + Style.RESET_ALL + "\nYour objective is to get as close as you can to " + Fore.YELLOW + "21" + Style.RESET_ALL + ".\n>Each round, you will be dealt two standard cards. You must add these and the person with the closest total wins. \n>You will choose whether to 'hit' or 'miss' every round. If you hit, you get dealt another card. However, be careful as going over " + Fore.YELLOW + "21 " + Style.RESET_ALL + "causes you to bust and lose! \n>Picture cards are worth 10 points each.\n>Aces are 1 or 11, randomly.")
  time.sleep(1)
else:
  print("")
rounds=int(input("How many rounds do you want to play " + name + "?"))
gamesLost=0
gamesWon=0
def twentyone():
  roun=0
  for i in range(0,rounds):
    gamesLost=0
    gamesWon=0
    roun+=1
    print("Round " + str(roun))
    cards=['Ace','Ace','Ace','Ace','2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5','6','6','6','6','7','7','7','7','8','8','8','8','9','9','9','9','10','10','10','10','Jack','Jack','Jack','Jack','Queen','Queen','Queen','Queen','King','King','King','King']
    cardVal = {'Ace':random.randint(1,11),'King':10,'Queen':10,'Jack':10,'10':10,'9':9,'8':8,'7':7,'6':6,'5':5,'4':4,'3':3,'2':2}
    card1=random.choice(cards)
    cards.remove(card1)
    card2=random.choice(cards)
    cards.remove(card2)
    hands=(str(card1) + ", " + str(card2))
    playerHand = cardVal[card1]+cardVal[card2]
    print("Your hand is " + hands + ". Your hand is worth " + str(playerHand) + ".")
    hit=""
    while hit!="H" and hit!="S" and hit!="h" and hit!="s":
      hit=str(input("Do you want to hit (H) or stay (S)?"))
      if hit!="H" and hit!="S" and hit!="h" and hit!="s":
        print("That is not a valid answer.")
      else:
        time.sleep(0.01)
    while hit=="H" or hit=="h":
      card=random.choice(cards)
      cards.remove(card)
      playerHand+=cardVal[card]
      hands=(hands + ", " + str(card))
      print("Your hand is " + hands + ". Your hand is worth " + str(playerHand) + ".")
      if playerHand>21:
        break
      else:
        time.sleep(0.01)
      hit=str(input("Do you want to hit (H) or stay (S)?"))
    if playerHand>21:
      print("Sorry, you bust!")
      gamesLost+=1
      i+=1
    else:
      time.sleep(0.01)
    cpuCard1=random.choice(cards)
    cards.remove(cpuCard1)
    cpuCard2=random.choice(cards)
    cards.remove(cpuCard2)
    handsCPU=(str(cpuCard1) + ", " + str(cpuCard2))
    computerHand=cardVal[cpuCard1]+cardVal[cpuCard2]
    while computerHand<17:
      if computerHand<17:
        cpuCard=random.choice(cards)
        cards.remove(cpuCard)
        computerHand+=cardVal[cpuCard]
        handsCPU=(handsCPU + ", " + str(cpuCard))
      else:
        time.sleep(0.01)
    if computerHand>21:
      print("I'm bust! You win.")
      gamesWon+=1
      i+=1
    elif computerHand>playerHand:
      print("My hand is " + str(handsCPU) + ". It is worth " + str(computerHand) + ". You lose.")
      gamesLost+=1
      i+=1
    else:
      print("My hand is " + str(handsCPU) + ". It is worth " + str(computerHand) + ". You win.")
      gamesLost+=1
      i+=1
twentyone()
print("\nGame Over!")
print("You won " + str(gamesWon) + " and lost " + str(gamesLost) + ".")
again=str(input("Do you want to go again? Y/N"))
if again=="Y":
  rounds=int(input("How many rounds do you want to play " + name + "?"))
  twentyone()
elif again=="N":
  print("Have a nice day.")
else:
  print("Have a nice day.")

1 个答案:

答案 0 :(得分:2)

不幸的是我不能自己测试,因为我没有所有的模块。但我认为你的问题是线条打印表明你每次赢了并输了0场比赛。

看起来你正在重置每个新游戏的游戏计数器:

for i in range(0,rounds):
    gamesLost=0
    gamesWon=0

你玩一个游戏,记录游戏胜利和失败,然后在循环重新开始时将下一个游戏重置为0。因为你也在循环之前初始化两个计数器 - 但也在def之外 - 如果你删除两行,它应该可以工作。或者您可以在循环之前移动行,如果冗余则可以删除另一个副本。但是,在函数内部定义计数器是更好的方法。