146k views
0 votes
Write a program that generates 1,000 random integers between 0 and 9 and displays the count for each number. (Hint: Use a list of ten integers, say counts, to store the counts for the number of 0s, 1s, ..., 9s.)

User Murali
by
5.2k points

1 Answer

3 votes

Answer:

import random

count0, count1, count2, count3,

count4, count5, count6, count7,

count8, count9, i = [0 for _ in range(11)]

while i < 1000:

number = random.randint(0,9)

if number == 0:

count0 = count0 + 1

if number == 1:

count1 = count1 + 1

if number == 2:

count2 = count2 + 1

if number == 3:

count3 = count3 + 1

if number == 4:

count4 = count4 + 1

if number == 0:

count5 = count5 + 1

if number == 6:

count6 = count6 + 1

if number == 7:

count7 = count7 + 1

if number == 0:

count8 = count8 + 1

if number == 9:

count9 = count9 + 1

i = i+1

print("0's: "+ str(count0) + "\\"+ "1's: "+ str(count1) + "\\"+

"2's: "+ str(count2) + "\\"+ "3's: "+ str(count3) + "\\"+

"4's: "+ str(count4) + "\\"+ "5's: "+ str(count5) + "\\"+

"6's: "+ str(count6) + "\\"+ "7's: "+ str(count7) + "\\"+

"8's: "+ str(count8) + "\\"+ "9's: "+ str(count9) + "\\")

Step-by-step explanation:

- Initialize variables to hold the count for each number

- Initialize i to control the while loop

- Inside the while loop, check for the numbers and increment the count values when matched.

- Print the result

User Kokeksibir
by
4.9k points