1

我要做的就是将 PlayerOne 的随机生成数字与 PlayerTwo 的数字进行比较。数字在 1 到 13 之间。每次有人获胜,他们都会得到 1 分。第一个到 10 的玩家是赢家。我已经为每个玩家生成了第一个随机数,并创建了一个得分图表,将获胜者加 1。我不明白如何通过单击返回按钮而不是自动生成两次。另外,我不明白如何使我制作的得分表自动了解哪个玩家获胜并为获胜玩家加分。谢谢。

import random


for PlayerOne in range(1):
    Score = 1
    PlayerOne = random.randint(1, 13)
    print(("Player One: %s" % PlayerOne))

    for PlayerTwo in range(1):
        PlayerTwo = random.randint(1, 13)
        print(("Player Two: %s" % PlayerTwo))


    if PlayerOne > PlayerTwo:
        print("Player One wins!")
        print(("Player One: %s" % Score))
        print("Player Two: 0")

    else:
        print("Player Two wins!")
        print("\nScore:")
        print("Player One: 0")
        print(("Player Two: %s" % Score))
4

2 回答 2

3

考虑您的代码片段:

for PlayerTwo in range(1):
  PlayerTwo = randint()
  print PlayerTwo

range(1)等价于[0],例如一个列表,其中一个元素的值为零。因此,您的 for 循环只执行一次,将值 0 分配给变量 PlayerTwo。随后,您用其他整数覆盖此变量。

其他人建议您查看 for 循环如何工作的原因是您的 for 循环中的代码只执行一次,这可能不是您想要做的。让你感到困惑的可能不是 for 循环,它可能是range.

因为您不知道发生的确切游戏数量,所以 for 循环可能并不理想。

这是我将如何解决此问题的伪代码(不是真实代码)。尝试了解我为什么使用while而不是for

while p1score < 10 and p2score < 10:
  p1 = randint()
  p2 = randint()
  if p1 > p2:
    p1score++
  elif p2 > p1:
    p2score++
于 2013-03-13T14:38:13.673 回答
0

我想我也明白了,谢谢 aestrivex!如果有人看到有任何问题或我看起来不正确的东西,请告诉我。

import random

input ('Please press "Enter" to begin the automated War card game.')

PlayerOneScore = 0
PlayerTwoScore = 0

while PlayerOneScore < 10 and PlayerTwoScore < 10:
    PlayerOne = random.randint(1, 13)
    PlayerTwo = random.randint(1, 13)
    if PlayerOne > PlayerTwo:
        PlayerOneScore += 1


    elif PlayerTwo > PlayerOne:
        PlayerTwoScore += 1



    print("Player One: ",PlayerOne)
    print("Player Two: ",PlayerTwo)
    print("\nScoreboard:")
    print("Player One Score: ",PlayerOneScore)
    print("Player Two Score: ",PlayerTwoScore,"\n\n\n")

    if PlayerOne > PlayerTwo:
        print("Player One Wins!")

    else:
        print("Player Two Wins!")
于 2013-03-13T15:17:04.003 回答