Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5 Yahtzee sdickey9480 Unladen Swallow Posts: 1 Threads: 1 Joined: Oct 2019 Reputation: 0 Likes received: 0 #1 Oct-01-2019, 03:41 AM 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() ``` ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 272 Likes received: 1264 #2 Oct-01-2019, 12:26 PM 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. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)