0
votes

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. 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.

  • If a one 1 is rolled, you get no score added and it's the next person's turn. If two 1's are rolled, you lose all of your points and its the next person's turn.

  • The first player to 100 scores, wins the game.

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

2 Answers

-1
votes

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?

-1
votes

Try changing the line

Player1 = p1dice+p1dice2

to

Player1 += p1dice+p1dice2

The old version replaces the value of Player1 each time. 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.