Question: Hey. I`m completely new to Python, and I am trying to get my head around lists. Got the basic around lists figured out, and I`m

Hey.

I`m completely new to Python, and I am trying to get my head around lists. Got the basic around lists figured out, and I`m trying to use functions and lists to solve a typical Yathzee dice throw. Got the basic dice throw ready.

import random 

def throw(dice=1, sides=6): result= [] for i in range(dice): numbers = random.randint(1,sides) result.append(numbers) return result

Now I want to write a function (throw2) that takes as a first parameter a list (dice) with dice roll of the type the function throw returns. As a second paramater, the function should take an integer parameter (save) with a value in range 1-6. The function makes a new roll with all the dice that do not have the same value as save, and updates the value in dice for the new roll.

I would imagine the in/output would be something like;

in: dice = [1,6,2,4,6]

in: throw2(dice, 6)

in: dice

out: [2,6,3,4,6] ect.

I`ve been trying to make a loop of the dice throws and

for i in range(len(throw)):

if throw[i] = save: throw[i] = randint(1,6)

but whatever I do, I cant seem to get it right.

Would appreciate a solution with a beginner-friendly explanation! :) As you probably figured, I can`t get any further in the yathzee game when I`m banging my head against this wall.

Sorry for bad english btw!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!