[英]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.