【问题标题】:Beginning my while loop in the two dice pig game in python在python中的两个骰子猪游戏中开始我的while循环
【发布时间】:2018-10-10 05:29:07
【问题描述】:

我目前正在用 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()

【问题讨论】:

    标签: python python-3.x while-loop dice


    【解决方案1】:

    换行试试

    Player1 = p1dice+p1dice2
    

    Player1 += p1dice+p1dice2
    

    旧版本每次都会替换Player1的值。新版本增加了它。

    顺便说一下,+=

    Player1 = Player1+p1dice+p1dice2
    

    Python 的许多其他运算符都有类似的“增强赋值”表示法。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 2014-06-24
        • 2020-07-24
        • 1970-01-01
        • 2015-02-03
        相关资源
        最近更新 更多