Where are the syntax errors?

Asked

Viewed 60 times

-4

I’m new to Python and the teacher is going through his classes too fast. Although it is ridiculous, I am in doubt what is wrong in the code below. I believe that the (basic) logic is not very wrong, but I am completely lost in the syntax. If you can help me, I really appreciate it! The purpose of the program is to find out how many shuttles would be destroyed. He will receive the total sum of the amount of petecas that three friends have, divide by the amount of friends and see how many petecas will remain. That rest is the amount of suckers that would be destroyed, to prevent friends from fighting.

def destruir_petecas(num_petecas, num_amigos = 3):
    """Escreva a instrução
    
    >>> destruir_petecas(91)
    1
    """
    pass

    destruir = destruir_petecas % num_amigos
    print (destruir)
  
print(
    destruir_petecas(91),
    destruir_petecas(91,7),
    destruir_petecas(91,7),
    

)

2 answers

1

The problem is on this line:

destruir = destruir_petecas % num_amigos

Notice that you are using the function destruir_petecas in the calculation, but the correct is to use the argument num_petecas:

def destruir_petecas(num_petecas, num_amigos = 3):
    return num_petecas % num_amigos

I also removed the pass, that it makes no sense to be there. It serves to indicate a "null operation", that is, it does nothing - see the documentation for more details.

And I made the function return the value instead of printing, because then who called the function do what you want with the return (can even print if you want). Printing the value inside the function and then trying to print out is redundant.

And since the function only returns the value of the calculation, nor does it need to keep it in a variable, it can return direct.

-2

It’s not wrong, but there’s a difference between print out the answer and return the answer:

print(destruir)
return destruir

The first command shows on the screen the contents of destruir and the second, returns to the line calling the function the value of destruir,

print (
    destruir_petecas(91),
    ...
)
  • 2

    https://meta.stackexchange.com/q/354782/176034

Browser other questions tagged

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