Python Forum
Yahtzee - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Yahtzee (/thread-21459.html)



Yahtzee - sdickey9480 - Oct-01-2019

The following program is a simplified version of Yahtzee, using only 3 die, and keeps a running total of your score vs the computer. Can anybody tell me why it is that when a Chance! occurs it isn't giving me the correct total (the sum of the 3 die faces)? Appreciated.


# @author Stephen Dickey


from random import randint


YAHTZEE = 50
TWO_OF_A_KIND = 25
NUM_DIE_SIDES = 6
die1 = randint(1, NUM_DIE_SIDES)
die2 = randint(1, NUM_DIE_SIDES)
die3 = randint(1, NUM_DIE_SIDES)
die = die1, die2, die3
CHANCE = 0
totalPointsPlayer = 0
totalPointsComputer = 0
count = 0

rollDice = input("Roll the dice (Y or N)? ")

while (rollDice.upper() == "Y" and count < 2):
    die1 = randint(1, NUM_DIE_SIDES)
    die2 = randint(1, NUM_DIE_SIDES) 
    die3 = randint(1, NUM_DIE_SIDES)
    CHANCE = die1 + die2 + die3
    if count == 0:
        print("Player rolls: ", die)
    else:
        print("Computer rolls: ", die)
    
    
    if count == 0:
        if (die1 == die2 == die3) :
            totalPointsPlayer += YAHTZEE
            print("Yahtzee! (+ %d)" % (YAHTZEE))
        elif ((die1 == die2) or (die1 == die3) or (die2 == die3)) :
            totalPointsPlayer += TWO_OF_A_KIND
            print("Two of a kind!(+ %d)" % (TWO_OF_A_KIND))
        elif ((die1 != die2) and (die1 != die3) and (die2 != die3)) :
            totalPointsPlayer += CHANCE
            print("Chance! (+ %d)" % (CHANCE))
        
    
    else: 
        if (die1 == die2 == die3) :
            totalPointsComputer += YAHTZEE
            print("Yahtzee! (+ %d)" % (YAHTZEE))
        elif ((die1 == die2) or (die1 == die3) or (die2 == die3)) :
            totalPointsComputer += TWO_OF_A_KIND
            print("Two of a kind!(+ %d)" % (TWO_OF_A_KIND))
        elif ((die1 != die2) and (die1 != die3) and (die2 != die3)) :
            totalPointsComputer += CHANCE
            print("Chance! (+ %d)" % (CHANCE))
        
        print()
        print("===========================")
        print("Player total points: %d " % (totalPointsPlayer))
        print("Computer total points: %d " % (totalPointsComputer))     
        print("===========================")
        print()    
        
    count = count + 1
    
    if count == 2:
        rollDice = input("Roll the dice again (Y or N)? ")
        if (rollDice.upper() == "Y"):
            count = 0
        print()     
           



RE: Yahtzee - ichabod801 - Oct-01-2019

That's not really the problem here. The problem is line 13: die = die1, die2, die3. Making that tuple is fine in and of itself, but later on (lines 27 and 29), you assume that as die1, die2, and die3 change, die will also change. This is not the case. Whatever the values of the three dice are at the beginning of the program, that's what they are for the rest of the program. Or maybe you're not making that assumption, and you just forgot to copy line 13 and put it after line 24, because that fixes your problem.