在 python 的两个骰子猪游戏中开始我的 while 循环

Beginning my while loop in the two dice pig game in python

我目前正在为 Python 3.6 中的骰子游戏编写代码 我知道我的编码在这方面有点偏离,但是,我真的只是想知道如何开始我的 while 循环。 游戏说明如下...

当我 运行 这段代码时,我一遍又一遍地得到相同的随机生成的数字。我不确定如何在每卷上获得不同的数字。我也不明白如何在每个玩家的回合结束时跟上他们的分数。 任何帮助都将不胜感激。

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的值。新版本加入了。

顺便说一下,+= 是 shorthand for

Player1 = Player1+p1dice+p1dice2

许多 Python 的其他运算符都有类似的 "augmented assignment" 表示法。

所以你的问题是随机数不能按你想要的那样工作而不是 "starting your loop"? 我真的只看到这种情况发生,因为你的系统时钟搞砸了(随机使用当前时间作为随机种子)。 您是否尝试过实例化 random.Random() 并从中调用?