[英]Beginning my while loop in the two dice pig game in python
我目前正在用Python 3.6編寫骰子游戲的代碼,我了解我的編碼對此有些欠缺,但是,我真的只是想知道如何開始while循環。 游戲說明如下...
人類玩家與計算機對戰。
他們輪流滾動兩個骰子,並且除非擲出1,否則將骰子的總和加在一起。
如果將一個1擲出,則不會添加任何分數,這是下一個人的回合。 如果擲出兩個1,則您將失去所有分,而下一局該回合。
第一個獲得100分的玩家贏得比賽。
當我運行此代碼時,我會反復獲得相同的隨機生成的數字。 我不確定如何在每卷上獲得不同的號碼。 我也不明白如何在每個回合結束時跟上每個球員的得分。 任何幫助將不勝感激。
import random
def main():
print("Welcome to the Two Dice Pig Game. You are Player 1!")
Player1 = 0
Player2 = 0
while(Player1<100 or Player2<100):
p1dice=random.randrange(1,7)
p1dice2=random.randrange(1,7)
Player1 = p1dice+p1dice2
print("Player 1 dice 1 =",p1dice)
print("Player 1 dice 2 =",p1dice2)
print("Player 1 dice total =",Player1)
print("Does player 1 want to hold?")
choose1 = input("Enter y for yes or n for no.")
if(choose1=="n"):
print("Player 1 dice 1 =",p1dice)
print("Player 1 dice 2 =",p1dice2)
print("Player 1 dice total =",Player1)
print("Does player 1 want to hold?")
choose1 = input("Enter y for yes or n for no.")
elif(choose1=="y"):
print("It's player 2's turn.")
print("Player 2 dice 2 =",p2dice)
print("Player 2 dice 2 =",p2dice2)
print("Player 2 dice total =",Player2)
print("Does player 2 want to hold?")
choose2 = input("Enter y for yes or n for no.")
main()
嘗試換線
Player1 = p1dice+p1dice2
至
Player1 += p1dice+p1dice2
舊版本每次都會替換Player1
的值。 新版本添加到其中。
順便說一句, +=
是
Player1 = Player1+p1dice+p1dice2
Python的許多其他運算符都有類似的“增強分配”符號。
因此,您的問題是隨機數不能像您想要的那樣工作,而不是像“開始循環”那樣工作? 我真的只看到這種情況發生,因為您的系統時鍾混亂了(隨機使用當前時間作為隨機種子)。 您是否嘗試實例化random.Random()並從中調用?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.