Doubt in a Python Exercise

Asked

Viewed 51 times

-1

I’m having doubts about an exercise.

  1. The program must read two integers called Min and Max. Min can be any value and Max must be greater than min. In then fill in a list with all values divisible by 7 contained in the closed range [Min,Max]. Display the resulting list in screen.

I started more I can’t get past this:

print('''Inicio do Programa''')

min = int (input('Digite um numero Minimo inteiro: '))
max = int (input('Digite um numero Maximo inteiro: '))

L = []
qtd = 0

while min and max / 7:

1 answer

3

The best thing for your problem is to use the function range(minValue, maxValue, step), because you can iterate perfectly as the description asks, and to check if a number is divisible by 7, just use the operator % entire split rest, if the rest of the split is 0 add the value to the list, using the method append(value).

print('''Inicio do Programa''')

minValue = int (input('Digite um numero Minimo inteiro: '))
maxValue = int (input('Digite um numero Maximo inteiro: '))

L = []

for i in range(minValue, maxValue + 1, 1):
    if (i % 7 == 0):
        L.append(i)

print(L);

Another option would also be using multiple range:

minValue = int(input('Digite um numero Minimo inteiro: '))
maxValue = int(input('Digite um numero Maximo inteiro: '))

print(*range(minValue + (7 - minValue % 7), maxValue + 1, 7))

It’s more compact but less readable. It works that way:
First multiple to minimum value partier = Min + (7 - Min % 7)
(7 - Min % 7) is how much missing from the minimum to the next multiple.
The heel (step) is equal to 7 why we have the guarantee that the first is a multiple. maxValue + 1 because the function range from 0 to maxValue - 1.

  • as well as multiples range?

  • @Ronalds, an Rage of multiples of 7 starting at a multiple of 7

  • got it thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.