1

This is one of the lab questions: I try to create a program that generates a list of N random integers between 0 and 19 and computes the element strictly less than 5, 10, 15 and 20. I want to print all of the 'There are {} elements between x and y' statements.

When I run the program, it only shows the first one, and not the others. How do I correct it?

from random import randint
import sys
while True:
    nb_of_elements = input('How many element do you want to generate? ')
    try:
        nb_of_elements = int(nb_of_elements)
        break
    except ValueError:
        print('Input is not an integer, try again...')

L = [randint(0, 19) for _ in range (nb_of_elements)]
print('The list is :', L)
number = [0] * 4
for i in range (nb_of_elements):
    number[L[i] // 5]+=1
for i in range(4):
    if number[i] < 5:
        print('There are {} elements between 0 and 4'.format (number[i]))
    elif 5<= number[i] < 10:
        print('There are {} elements between 5 and 9'.format(number[i]))
    elif 10<= number[i] < 15:
        print('There are {} elements between 10 and 14'.format(number[i]))
    else:
        print('There are {} elements between 15 and 20'.format(number[i]))
Kay
  • 37
  • 5

2 Answers2

0

Your mistake is that you're attempting to count numbers in a range twice. First, you use the trick with integer division:

for i in range (nb_of_elements):
    number[L[i] // 5]+=1

So, number already contains the count of elements in the ranges 0--4, 5--9, 10--14 and 15--19 (inclusive).

Then, in your if-elif-elif-else block, you look at the value of number, whether it fits in any of these ranges. number, however, contains counts. On average, it will contain about nb_of_elements / 5 counts for each element.

You don't need the if-elif-elif-else block. Instead, loop through range(4) as you do know, and print each element number[i]. Each time, it'll correspond to the next range (you may need some smart thing to print the range. 5*i and 5*i+4 may do that).


It's kind-of interesting that you came up with a smart way to count the numbers in a range (number[L[i]//5] += 1), and then fell back to standard range comparison in an if-elif-else chain. I guess one can outsmart oneself.

0

You already have found a smart way to count fill the nb_of_elements list. Now you may want a smart way to print it. You can use enumerate to get the current index in the for loop: with this index, you can create the 'between X and Y' variables.

counts = [0] * 4
for i in range (nb_of_elements):
    counts[L[i] // 5]+=1

# Loop the counts, and keep track of the index for enumerate
for i,count in enumerate(counts):
     # i * 5 will be [0,5,10,15] and i * 5 + 5 will be [5,10,15,20]
     print('There are {} elements between {} and {}'.format (count, i*5, i*5 + 5))

#The list is : [7, 10, 5]
#There are 0 elements between 0 and 5
#There are 2 elements between 5 and 10
#There are 1 elements between 10 and 15
#There are 0 elements between 15 and 20

In Python, a range is exclusive, meaning 'between 0 and 5' is [0,1,2,3,4]. I have chosen this notation for the print function as well: it now states 'between 0 and 5' (exclusive) instead of 'between 0 and 4' (inclusive) like you used in your code. This can of course be easily changed: i*5 + 5 > i*5 + 4.

Community
  • 1
  • 1
Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29