[英]I can't seem to figure out how to generate multiple random numbers to equal a score of 10
我要做的是将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))
考虑你的代码片段:
for PlayerTwo in range(1):
PlayerTwo = randint()
print PlayerTwo
range(1)
等价于[0]
,例如一个元素的值为零的列表。 因此,你的for循环只执行一次,将值0赋给变量PlayerTwo。 随后用其他整数覆盖此变量。
其他人建议您查看循环如何工作的原因是for循环中的代码只执行一次,这可能不是您想要做的。 它可能不是让你困惑的循环,它可能是range
。
因为您不知道发生的确切游戏数量,所以for循环可能并不理想。
这是我将如何解决这个问题的伪代码(不是真正的代码)。 试着去理解为什么我用while
并没有for
:
while p1score < 10 and p2score < 10:
p1 = randint()
p2 = randint()
if p1 > p2:
p1score++
elif p2 > p1:
p2score++
我想我觉得非常感谢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!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.