Home > Net >  Using dice as a kids game and summing random list of ints while excluding specific values from the f
Using dice as a kids game and summing random list of ints while excluding specific values from the f

Time:10-08

The game begins with the player 'rolling 5 dice.' Then the player gets a score of the combined numbers of the dice excluding 2's and 5's. Any 2's or 5's rolled results in dice "stuck in the mud." So say you roll a [1, 3, 4, 5, 2] the score should be: 8. Then the player rolls the remaining 3 dice, again you rolled [6,3,2] the score should then be 17. So on and so forth until the player runs out of dice and the code returns a final score.

import random

print("One round of Stuck in the Mude")
game = input("Enter r to roll the dice or q to quit: ")

diceC = 5
while game == "r" and game != "q" and diceC > 0:

    dice = [random.randint(1,6) for i in range(diceC)]

    score = 0
    for d in dice:
        # the rules for getting a score
        if d==2 or d==5:
            diceC -= 1
            pass
        else:
            score = score   d

    print('dice rolls:', dice)
    print('score', score)
    game = input("Enter r to roll the dice or q to quit: ")

CodePudding user response:

The attempt that you made was okay, but there was overuse of the lists (which probably confused you). In light of your comments, this would be a good clean solution to work from.

  • need one variable for the number of rolls.
  • only need one list (for the dice rolls).
  • for loop over the list to generate the score.

(You never needed to append the lists, but in any event, one needs to be careful appending lists that they are iterating through as it risks going out of range).

Here is the code:

import random

number_rolls = 5
dice = [random.randint(1,6) for i in range(number_rolls)]

score = 0
for d in dice:
    # the rules for getting a score
    if d==2 or d==5:
        pass
    else:
        score = score   d
        print('current score:', score)

print('---end---')
print('dice rolls:', dice)
print('final score', score)

result:

current score: 3
current score: 9
current score: 12
current score: 15
---end---
dice rolls: [3, 6, 3, 3, 5]
final score 15

CodePudding user response:

i add a new answer based on the comments to the last answer.

it appears that the OP requires this:

when a 2 or 5 is rolled, the dice is removed from the game and the other dice continue to be rolled until there are no dice left.

We can reduce the dice list with a list comprehension.

This keeps playing the game until there are no dice left.

Here is the code:

import random

score = 0
games_played = 0
number_rolls = 5

while True:
    games_played  = 1
    if games_played > 100:
        print('more than 100 games played: you won !!!')
        break

    # user input:
    play_game = input("Enter r to roll the dice or q to quit: ")
    if play_game == 'q':
        break
    

    dice = [random.randint(1,6) for i in range(number_rolls)]
    print('dice rolled:', dice)
    dice = [d for d in dice if d in [1,3,4,6]]  # remove 2 & 5
    number_rolls = len(dice)
    
    if len(dice) == 0:
        break

    for d in dice:
        score = score   d
        print('current score:', score)
        

print('---end---')
print('games played:', games_played)
print('final score', score)

example result:

Enter r to roll the dice or q to quit: r
dice rolled: [6, 6, 6, 3, 2]
current score: 6
current score: 12
current score: 18
current score: 21
Enter r to roll the dice or q to quit: r
dice rolled: [3, 3, 4, 5]
current score: 24
current score: 27
current score: 31
Enter r to roll the dice or q to quit: r
dice rolled: [6, 5, 4]
current score: 37
current score: 41
Enter r to roll the dice or q to quit: r
dice rolled: [5, 5]
---end---
games played: 4
final score 41
  • Related