function to return the integer number

Asked

Viewed 1,536 times

1

what I’m doing wrong in my job?

num2 = float(input("digite um número não inteiro: "))
def arrend(numero):
    if num2 - math.floor(num2) < 0.5:
        numero = math.floor(num2)
        return numero
        return math.ceil(num2)
print("o número inteiro correspondente é: {}".format(arrend))

2 answers

2

When you are calling your function back, inside your print, you are not passing the parameter that will be used. There is also an error in your return, your indentation is wrong, this way the second Return is inside the if, and will never run.

arrend(numero)

In this case, I think you want to pass the variable num2 as parameter. It would look something like this, arranging the return as well.

def arrend(numero):
    if num2 - math.floor(num2) < 0.5:
        numero = math.floor(num2)
        return numero
    return math.ceil(num2)
num2 = float(input("digite um número não inteiro: "))
print("o número inteiro correspondente é: {}".format(arrend(num2)))
  • hi @Vinicius Macelai it only worked for Math.floor but it didn’t work for Math.Ceil

  • @Márciofeitosa it returns only once, so it only does the number Return command, to return the two values, look at my new answer.

  • It keeps not returning anything to Ceil and now when the number should go to floor it returns both (floor and Ceil)

  • Ah, I understood your problem, you wanted if it failed the if condition, return to Ceil, edited again,

  • You were the guy in that...

0

To use the functions of the methods floor and ceil, you have to first import the methods.

Another thing, when we work with funções we should know how implementa-las.

Note below the solution of the question...

from math import floor, ceil


def arrend(num2):
    if num2 - floor(num2) < 0.5:
        return floor(num2)
    return ceil(num2)


num = float(input("digite um número não inteiro: "))
print("o número inteiro correspondente é: {}".format(arrend(num))) 

Note that when we execute this code we receive the message: Digite um número não inteiro: . Right now we must enter a real number and press enter. At this point the value captured by the input is passed as a parameter to the function arrend(). Getting there, the block if will check whether n - floor(num2) < 0.5. If the check is positive the return of the function will be floor(num2). Otherwise, the function return will be ceil(num2).

Browser other questions tagged

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