Strange return when calling a function

Asked

Viewed 107 times

1

I’m trying to do an exercise on functions, in Python, with the following statement: "Make a program that converts from 24-hour notation to 12-hour notation". Example: Convert 14:25 to 2:25 PM. The program must contain two functions, one that converts the time and one that prints the time (with AM or PM).

I’ve reached a certain point, but now I’m in doubt as to why the program isn’t running.

My show:

hora = int(input("Digite a hora: "))

minuto = int(input("Digite os minutos: "))

def converter_hora(hora):

         return (hora -12)

def imprime_hora(hora,minuto):

        if(hora <= 12):
           print(hora,minuto,"AM")
        else:
           print(converter_hora,minuto,"PM")

print(imprime_hora)

Python is returning the following message:

Function imprime_hora at 0x02D8C0C0

Any suggestions?

1 answer

2


To make a call from a function, you must put the name of the function, followed by parentheses. If the function has arguments, the values of these should be specified among the (). In your case, you want to invoke the function imprime_hora, who has the arguments hora and minuto, then the function call should be something like:

print( imprime_hora(14, 25) )

As, in this case, the values of hora and minuto are read from the user, you must do:

print( imprime_hora(hora, minuto) )

The same happens when you invoke the function converter_hora in:

print(converter_hora,minuto,"PM")

You must enter the value of the argument hora in this way:

print(converter_hora(hora), minuto, "PM")

Moreover, considering that the function itself imprime_hora already prints the result, no need to do print( imprime_hora(14, 25) ), just do imprime_hora(14, 25).

The final code, already correcting the indentation errors, stays:

hora = int(input("Digite a hora: "))
minuto = int(input("Digite os minutos: "))

def converter_hora(hora):
    return (hora -12)

def imprime_hora(hora,minuto):
    if(hora <= 12):
        print(hora,minuto,"AM")
    else:
        print(converter_hora(hora), minuto, "PM")

imprime_hora(hora, minuto)

Browser other questions tagged

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