Calling functions by shell

Asked

Viewed 259 times

0

Write the function maior_primo that takes an integer greater than or equal to 2 as a parameter and returns the largest prime number less than or equal to the number passed to the function.

Examples of Python shell execution:

>>> maior_primo(100)
97

>>> maior_primo(7)
7

Tip: write a function éPrimo(k) and make a loop through the numbers to the given number checking whether the number is prime or not; if it is, keep it in a variable. At the end of the loop, the value stored in the variable is the largest prime found.

For IDE I did it in a way and it worked, but the exercise requires that it is for shell and it doesn’t work.

My code is like this:

#-----------------------------------------------------
def eh_primo(n):

    primo = True
    i = 2
    while i <= n/2 and primo:
        if n % i == 0:
            primo = False
            n = int(input("Digite um inteiro: "))
        i += 1

    return primo

#-----------------------------------------------------

def maior_primo(x, y):
    if eh_primo(x > y):
        return x
    else:
        return y
  • 1

    Could you explain better what you mean by "called by the shell"? In your role maior_primo, the expression eh_primo(x > y) doesn’t make sense; what did you want to do here? And realize that function maior_primo should receive only one parameter, why set with two? Who are x and y in that context?

  • The exercise proposes to compare two integers that are prime and need two functions, one to check if the number and prime and one between which is the largest, would run the script by the standard python shell, and make a call from it for example: maior_primo(11, 23)And he should check if both parameters are prime and which of them is the largest and if one of the parameters is not prime the user should enter a new value until it is prime and after becoming prime he check which is the largest

  • 1

    I think you’ve got the problem wrong. It asks that the input be an integer and that the return be the largest prime number that is less than or equal to the input value. You don’t need to compare two prime numbers. The two functions do, in fact, but I recommend that you do the table test; You don’t seem to understand what your own code does, maybe the test will help you with that. Another thing that made no sense was to read the value of n within the function eh_primo. If you receive it by parameter, why do you need to read it again?

No answers

Browser other questions tagged

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