Math, asked by jpsavita5771, 1 year ago

write a random number generator that generates random numbers between 1 and 6 (simulates a dice).

Answers

Answered by rishika79
2

Answer:

Step-by-step explanation:

import random

def startTheGame():

mylist=[random.randint(1, 6) for _ in range(6)]

return mylist

List comprehensions are among the most powerful tools offered by Python. They are considered very pythonic and they make code very expressive.

Consider the following code:

counter = 0

myList = []

while (counter) < 6:

randomNumber = random.randint(1, 6)

myList.append(randomNumber)

counter = counter + 1

if (counter)>=6:

pass

else:

return

We will refactor this code in several steps to better illustrate what list comprehensions do. First thing that we are going to refactor is the while loop with an initialization and an abort-criterion. This can be done much more concise with a for in expression:

myList = []

for counter in range(6):

randomNumber = random.randint(1, 6)

myList.append(randomNumber)

And now the step to make this piece of code into a list comprehension: Move the for loop inside mylist. This eliminates the appending step and the assignment:

[random.randint(1, 6) for _ in range(6)]

The _ is a variable name just like any other, but it is convention in python to us e _ for variables that are not used. Think of it as a temp variable.

Hope it helps you...

Similar questions