How to generate a range from A to B-1 in Python?

Asked

Viewed 693 times

0

Here is my code to print if a number is prime or not, but it is necessary that the number printed by the user is not included in the block if, how could I do to establish an interval in the for in range(2,numero_digitado_pelo_usuario) so that I don’t analyze the user’s own number typed in the block if previously mentioned.

numero = int(input('Digite seu numero aqui : '))

for x in range(2,numero+1):
        if numero % x != 0 :
                print('primo')
        else :
                print('não primo')

So I wanted to find a way to solve this problem, thank you for the solution!

  • I notice that your question has a lot to do with your previous question.

  • Your algorithm does not check if the number is prime. The messages in your code are wrong. As Victor Stafusa showed you just check if numero is or is not divisible by x.

2 answers

3


The answer is for x in range(2, numero):.

The first number in range is the first value that is considered to be inside it. The second number is the first that is considered to be outside. Then, range(a, b) goes from a until b - 1. Soon range(2, numero) goes from 2 until numero - 1.

For example:

numero = 7

for x in range(2, numero):
    if numero % x != 0:
        print(str(numero) + ' não é divisível por ' + str(x))
    else:
        print(str(numero) + ' é divisível por ' + str(x))

numero = 6

for x in range(2, numero):
    if numero % x != 0:
        print(str(numero) + ' não é divisível por ' + str(x))
    else:
        print(str(numero) + ' é divisível por ' + str(x))

Generates the following output:

7 não é divisível por 2
7 não é divisível por 3
7 não é divisível por 4
7 não é divisível por 5
7 não é divisível por 6
6 é divisível por 2
6 é divisível por 3
6 não é divisível por 4
6 não é divisível por 5

See here working on ideone.

  • It still doesn’t work, I want the very number typed by the user not to be in the if command so identify the prime number, since I eliminated the 1 of the command is and now I want to eliminate the very number inserted so that all the divisions carried out are different from 0 and thus result in prime number.

  • @kvojps I edited the answer and showed it running when the number is 7 (it goes from 2 to 6) and when it is 6 (goes from 2 to 5). The test clearly shows that the number itself is excluded yes.

  • Thank you very much my dear

0

From what I understand you want to implement a code that is able to verify if a certain number is cousin.

According to the definition, prime numbers sane: natural numbers greater than 1 that have only two divisors, i.e., are divisible by 1 and by itself.

One of the correct ways to verify that a certain number is prime is by using the following code::

def verifica_primo(n):
    i = 1
    cont = 0
    while i <= n:
        if n % i == 0:
            cont += 1
        i += 1
    if cont != 2:
        return False
    else:
        return True


num = int(input('Digite um número inteiro: '))

print(verifica_primo(num))

Note that when we execute this code we receive the following message: Digite um número inteiro: . Right now we must enter an integer number and press enter. Then the variable value num is passed as parameter n for the function verifica_primo(n). Getting there, the block while shall traverse the closed interval [i, n], where i starts with the value 1 going to the maximum value n. With the help of 1st block if will be verified whether i is divider of n. If positive, the unit will be accumulated in the variable cont. Subsequently, the 2nd block if check the value of the variable cont. If its value is other than 2, the function will return False and otherwise the function will return True. In other words, if the number typed is prime, the output will be True, otherwise the output will be False.

Browser other questions tagged

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