I can't understand this block

Ey guys I’m learning pythoin using w3school actually my experience it’s beginner. I’m prcaticing with the try: except: block and I can’t resolve this:

 def manejoErrores(valor):
    try:
        valor = float(valor)
    except:
        print("error")
    return calcular(valor)

def mensaje():
    income = input("Ingrese el monto total: ")
    return manejoErrores(valor)

x = mensaje()
print(x)

It’s suppose that code recive a amount from a invoice, originally is a string and I try to convert in a float yo continue when I insert number as the beginning the block works but when I insert string to prove the function and try to insert values again it doesn’t work

In your original function, try updating to:

def mensaje():
    valor = input("Ingrese el monto total: ")
    return manejoErrores(valor)

By the way, where is the function calcular defined from the first function? Anytime that you use a variable or a function, or any object for that matter must either first be assigned or defined. Here, calcular is not defined so you will still be getting an error.

Exactly what happens when you try the code?

Exactly what do you think should happen instead, and how is that different?

There are a few things to comment on in this code. Taking your mensaje
function first:

 def mensaje():
     income = input("Ingrese el monto total: ")
     return manejoErrores(valor)

This calls manejoErrores(valor) but does not set a value for valor.
And the value in income, which came from input(), is not used. I
suspect you should be passing income to the manejoErrores()
function.

 def manejoErrores(valor):
     try:
         valor = float(valor)
     except:
         print("error")
     return calcular(valor)

What should the return value of this function be? For valid values of
valor I imagine it should be calcular(valor). But what should happen
when valor is not a valid “float string”?

Presently this just prints "error". But then execution falls through
and runs the return statement anyway, which doubtless raises its own
exception because valur if still a string.

Also, try to always avoid a “bare except”, this line:

 except:

This catches any exception. For example, if you type a variable name
wroing Python will raise a NameError exception and your code will also
catch that. Catch only what you expect to handle: to accomodate in
some way. So for float() with a bad number string you should expect a
ValueError, and only catch that:

 except ValueError:

Cheers,
Cameron Simpson cs@cskk.id.au

Hey guys, I’m appreciate your answer I just wanted to encapsulate everything in functions to manage the messages to the user and los procesos. it’s just a practice, and this morning I’ve started everything and in the function mensaje() I did the comprobations I like to separate both process because my original idea was drive messages and comprobations in different process,

The final code its:


"""practice #5"""


def calculateinvoice(fvalue):
    """function to receive the real value and 
       return final values"""
    return fvalue


def message():
    """function to receive the invoice's value and 
       validate for errors
    """
    err = True
    while err:
        try:
            value = float(
                input("Enter the total of your invoice: "))
            err = False
        except ValueError:
            print("Enter correct values,")

    return value


vvalue = message()
nvalue = calculateinvoice(vvalue)
print(nvalue)

It’s deffinetly not the best code I’d like to do a function for process if there are something to improve let me know. Thank you Guys!