Reputation: 5
I've been modifying a code for the dice game pig. I modified it to where it rolls 2 dice instead of one. When you roll a one it goes to the next players turn and none of your points stay the same from your previous score. Well when you roll Two ones I need it to reset the score to 0 and print out my message "Snake Eyes!! your score is set to 0". The problem is both messages print out for snakes eyes and when you roll just a one, when two ones are rolled. Also the score does not reset.
EDIT* I've fixed the code with the conflicting print messages, but Still having trouble getting the score to reset to 0 when snakeyes are rolled
from random import randint
playercount = 2
maxscore = 100
safescore = [0] * playercount
player = 0
score=0
while max(safescore) < maxscore:
if player == 0:
rolling = 0
if score < 17 and score + safescore[player] < maxscore:
rolling = 1
else:
rolling = input("Player %i: (%i, %i) Rolling? (Y) "
% (player, safescore[player], score)).strip().lower() in {'yes', 'y', ''}
if rolling:
rolled = randint(1, 6)
rolled2 = randint(1, 6)
print(' Rolled %i' % rolled)
print(' Rolled %i' % rolled2)
if rolled ==1 and rolled2 ==1:
print(' Snake Eyes!! your score is set to 0')
score=0
elif rolled == 1:
print(' Bust! you lose %i but still keep your previous %i'
% (score, safescore[player]))
score, player = 0, (player + 1) % playercount
elif rolled2 == 1:
print(' Bust! you lose %i but still keep your previous %i'
% (score, safescore[player]))
score, player = 0, (player + 1) % playercount
else:
score += rolled + rolled2
else:
safescore[player] += score
if safescore[player] >= maxscore:
break
print(' Sticking with %i' % safescore[player])
score, player = 0, (player + 1) % playercount
print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))
Upvotes: 0
Views: 478
Reputation: 20173
You need to correct the order of your checks. Right now your "rolled a single one" check comes before the "rolled two ones" check, so it will execute when two ones are rolled. If you put the "two ones" check first and make the two exclusive with an else-if construct, you will get the behavior you want:
if rolled==1 and rolled2==1:
# snake eyes
elif rolled==1:
# a single one
else:
# all other cases
Also, note that you are currently only checking if both dice came up one, or if the first die came up one. You'll probably want to also check if the second die came up one.
Upvotes: 2