Python 3.x 如何编写一个程序,将一副牌的一半发给一个玩家,另一半发给另一个玩家?

Python 3.x 如何编写一个程序,将一副牌的一半发给一个玩家,另一半发给另一个玩家?,python-3.x,Python 3.x,我正试图为战争游戏编写代码,但目前我的代码有两个问题 第一个问题是,当两张牌的值相同时(在dealWar()函数中),我无法让我的游戏开战。它正在决定一个胜利者,但我不知道它是怎样的 我遇到的第二个问题是,我正在尝试编写一个函数,该函数将确定最终分数并确定胜利者。我现在编写的代码finalScore()似乎并没有计算所有说playerne+“赢!”的字符串,它只是暂时停止 import random # deck of cards frontOfCard = [] suits = ["Hea

我正试图为战争游戏编写代码,但目前我的代码有两个问题

第一个问题是,当两张牌的值相同时(在
dealWar()
函数中),我无法让我的游戏开战。它正在决定一个胜利者,但我不知道它是怎样的

我遇到的第二个问题是,我正在尝试编写一个函数,该函数将确定最终分数并确定胜利者。我现在编写的代码
finalScore()
似乎并没有计算所有说
playerne+“赢!”
的字符串,它只是暂时停止

import random

# deck of cards
frontOfCard = []

suits = ["Hearts", "Diamonds", "Clubs", "Spades"]

royals = ["Ace", "Jack", "Queen", "King"]

oldDeck = []


for numbers in range(2,11):
  frontOfCard.append(str(numbers))

for royal in range(4):
  frontOfCard.append(royals[royal])

for suit in range(4):
  for card in range(13):
    cards = (frontOfCard[card] + " of " + suits[suit])
    oldDeck.append(cards)

#create players
def nameOfPlayers():
  print("Welcome to the Game of WAR! " + playerOne + " and " + playerTwo)

#explanation of game of war
def explanation():
  print("Here are the rules!")
  print("The deck is divided evenly to each player, with each player receiving 26 cards.")
  print()
  print("The cards will be flipped for the playeres and the player with the higher card takes both cards.")
  print()
  print("If the cards are the same rank, it is WAR.  Each player turns up one card face down and one card face up.  The player with the higher card takes both piles, if they are the same rank,each players places another card face down and turns the other face down card up, continue until the winner of that pile is determined.")

#Deal out cards
#Fix code to go into war if cards are the same
def dealWar():
  random.shuffle(oldDeck)
  hand1 = oldDeck[0:int(len(oldDeck)/2)]
  hand2 = oldDeck[int(len(oldDeck)/2):len(oldDeck)]
  for i in range(0, int(len(oldDeck)/2)):
    handOne = hand1.pop()
    handTwo = hand2.pop()
    print(playerOne + " has played: " + handOne + "  |  " + playerTwo + " has played: " + handTwo)
    if handOne > handTwo:
      print(playerOne + " Wins!")
      print()
    elif handOne == handTwo:
      print("WAR has BEGUN!")
      handOne = hand1.pop()
      handTwo = hand2.pop()
      print(playerOne + " has played: " + handOne + "  |  " + playerTwo + " has played: " + handTwo)
      if handOne > handTwo:
        print(playerOne + " Wins!")
      else:
        print(playerTwo + " Wins!")
        print()
    else:
      print(playerTwo + " Wins!")
      print()

# fix to count every word that says player name and wins
def finalScore():
  playerOneScore = len(playerOne + " Wins!")
  playerTwoScore = len(playerTwo + " Wins!")
  if playerOneScore > playerTwoScore:
    print(playerOne + " ! ")
  elif playerOneScore == playerTwoScore:
    print("NO ONE, the game ended as a DRAW! ")
  else:
    print(playerTwo + " ! ")
#CODE

playerOne = input("What is your name? ")
playerTwo = input("What is your name? ")
print()

nameOfPlayers()

print()

explanation()
print()

userInput = input("Are both players ready to start? (y/n): ")
print()

if userInput == "y":
  print("Here we go! ")
  print()
elif userInput == "n":
  print("Too bad we're starting anyways")
else:
  print("Wrong Input, Try Again!")

print()

dealWar()

print("The winner is...........")
finalScore()
print()

playAgain = input("Would you like to play again? (y/n): ")
if playAgain == "y":
  dealWar()
  print()
  finalScore()
  print()
  print(playAgain)
  print()
  print("The winnder is.......")
  print()
  finalScore()
else:
  print("GOOD BYE")

对于战争,你可以在洗牌后使用列表切片将牌组分成两个相等的部分

hand1 = deck[0:int(len(deck)/2)]
hand2 = deck[int(len(deck)/2):len(deck)]
在这两行之后,
hand1
将包含从索引0到牌组中间(不包括)的牌,
hand2
将包含从牌组中间(包括)到牌组末端的牌

如果你想模拟从牌组中发牌的动作,你可以在牌组上循环并将牌附加到每只手上,同时将牌从牌组中弹出

for i in range(0, int(len(deck)/2)):
    hand1.append(deck.pop())
    hand2.append(deck.pop())

对于战争,你可以在洗牌后使用列表切片将牌组分成两个相等的部分

hand1 = deck[0:int(len(deck)/2)]
hand2 = deck[int(len(deck)/2):len(deck)]
在这两行之后,
hand1
将包含从索引0到牌组中间(不包括)的牌,
hand2
将包含从牌组中间(包括)到牌组末端的牌

如果你想模拟从牌组中发牌的动作,你可以在牌组上循环并将牌附加到每只手上,同时将牌从牌组中弹出

for i in range(0, int(len(deck)/2)):
    hand1.append(deck.pop())
    hand2.append(deck.pop())

使用len(oldDeck)获取列表oldDeck的长度,然后通过:firstHalf=oldDeck[0:int(length/2)]和secondHalf=oldDeck[int(length/2):]获取列表oldDeck的长度(oldDeck),然后通过:firstHalf=oldDeck[0:int(length/2)]和secondHalf=oldDeck[int(length/2:]获取前半部分我已经更新了我的代码,现在有一个问题。当我
print(hand1)
时,它显示了26张卡片的整个卡片组,而不是显示弹出的1张卡片。@ChamongLo我显示的两种不同方法将分别工作。你应该选择一个或另一个,但不是两个。我已经更新了我的代码,现在有一个问题。当我
print(hand1)
时,它显示了26张卡片的整个卡片组,而不是显示弹出的1张卡片。@ChamongLo我显示的两种不同方法将分别工作。你应该选一个或另一个,但不能两者都选。