简体   繁体   English

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

[英]Beginning my while loop in the two dice pig game in python

I'm currently writing code for a dice game in Python 3.6 I understand my coding is a little off in this, however, I'm really just wondering how to start my while loop. 我目前正在用Python 3.6编写骰子游戏的代码,我了解我的编码对此有些欠缺,但是,我真的只是想知道如何开始while循环。 The instructions of the game are as follows... 游戏说明如下...

  • A human player plays against the computer. 人类玩家与计算机对战。

  • They take turn rolling two dice, and the totals of the dice are added together Unless a 1 is rolled. 他们轮流滚动两个骰子,并且除非掷出1,否则将骰子的总和加在一起。

  • If a one 1 is rolled, you get no score added and it's the next person's turn. 如果将一个1掷出,则不会添加任何分数,这是下一个人的回合。 If two 1's are rolled, you lose all of your points and its the next person's turn. 如果掷出两个1,则您将失去所有分,而下一局该回合。

  • The first player to 100 scores, wins the game. 第一个获得100分的玩家赢得比赛。

When I run this code, I get the same randomly generated number's over and over. 当我运行此代码时,我会反复获得相同的随机生成的数字。 I am not sure how to get different number's on each roll. 我不确定如何在每卷上获得不同的号码。 I also don't understand how to keep up with each player's score at the end of their turn's. 我也不明白如何在每个回合结束时跟上每个球员的得分。 Any help at all would be greatly appreciated. 任何帮助将不胜感激。

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()

Try changing the line 尝试换线

Player1 = p1dice+p1dice2

to

Player1 += p1dice+p1dice2

The old version replaces the value of Player1 each time. 旧版本每次都会替换Player1的值。 The new version adds to it. 新版本添加到其中。

By the way, the += is a shorthand for 顺便说一句, +=

Player1 = Player1+p1dice+p1dice2

Many of Python's other operators have a similar "augmented assignment" notation. Python的许多其他运算符都有类似的“增强分配”符号。

So your problem is that the random numbers don't work like you want rather than something about "starting your loop"? 因此,您的问题是随机数不能像您想要的那样工作,而不是像“开始循环”那样工作? I really only see this happening because your system clock is messed up(random uses the current time as seed for random). 我真的只看到这种情况发生,因为您的系统时钟混乱了(随机使用当前时间作为随机种子)。 Have you tried instantiating random.Random() and calling from that? 您是否尝试实例化random.Random()并从中调用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM