Questions about functions

0

This my code simulates banking using functions, but I am not able to return the values of the function, when I hedge the value of the return in a variable it calls the whole function. In the program when I call the 'balance' function, it returns me the 'deposit' function and not the 'amount' value.

Any guidance? How can I do this? Is there a better way?

def deposito(dep):
    clear()
    montante=0.0
    dep=0.0
    dep=float(input("Quantia a ser depositada:"))
    clear()
    if (dep > 0):
        print (" |",montante)
        print ("+|",dep)
        montante=montante+dep
        print ("____________")
        print (" |",montante,"\n\n")
    else:
        print("Quantia inválida")
    voltar()
    return montante

def saldo():
    saldo=deposito()
    print("O seu saldo é:", saldo())


def main():
    op=0
    while(op != range(0,4,1)):
        clear()
        print("Qual tipo de operação deseja realizar?\n")
        print ("[1]Saldo")
        print ("[2]Depósito")
        print ("[3]Saque")
        print ("[0]Sair\n")
        op=str(input(""))


        if(op == "1"):
            saldo()
        elif(op == "2"):
            deposito()
        elif(op == "3"):
            saque()
        elif(op == "0"):
            exit
        else:
            clear()
            print("Operação inválida")
            time.sleep(1)   
    
asked by anonymous 22.09.2017 / 07:16

1 answer

1

The functions you have are not being defined / called correctly, as well as other small construction errors.

  • At the top, the deposito function was set to receive a value:

    def deposito(dep):
    

    This dep value is not passed in the main where this function is called:

    elif(op == "2"):
        deposito()
    

    You have two options, or you can read the value in main and go to the deposito function, or it does not pass any value and reads the value inside the deposito function. Right now you're doing both. The simplest correction will be to remove the parameter from the function definition, leaving it as:

    def deposito():
    
  • In function saldo is catching a float and trying to use it as if it were a function

    def saldo():
        saldo=deposito()
        print("O seu saldo é:", saldo()) #<-- aqui em saldo()
    

    The function deposito() returns a float so just show this float directly, like this:

    print("O seu saldo é:", saldo) #agora sem os ()
    
  • montante is not global all functions will soon not accumulate as you perform the various operations. To make global you need to put it before the function and use the reserved word global within the function:

    montante = 0.0
    
    ...
    
    def deposito():
        clear()
        global montante #aqui indica que está a utilizar o montante global fora da função
    
22.09.2017 / 19:58