Doubts about functions

Asked

Viewed 138 times

1

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

Any guidance? How can I do that? There’s 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)   
  • 1

    I think the code is having some problems in formatting. At least on my mobile the function deposito and your first command are at the same indentation level

  • If you want to know op is not in the range that varies [0.4), its condition of the while of function main isn’t doing that

  • saldo is in infinite recursion? Actually I can’t predict the behavior of Python in this case, although I think it will tell you that the parameter dep has no standard value, or that float is not executable

  • 1

    Reinforcing what @Jeffersonquesado said, confirm that the identation of the code here in the question is the same as it has. If it is not correct.

  • 2

    Dei rollback in the edition that applied indentation, taking into account that it, if not done correctly, can affect the functioning of python code.

  • Done the code identation correction here, in the program is correct.

Show 1 more comment

1 answer

1

The functions you have are not being set/called correctly, just as there are other small construction errors.

  • At the top the function has been defined deposito to receive a value:

    def deposito(dep):
    

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

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

    Have two options or read the value in main and becomes a function deposito, or does not pass any value and reads the value within the function deposito. Right now you are doing both. The simplest correction will be to remove the parameter from the function setting, leaving it as:

    def deposito():
    
  • In function saldo is taking a float and try to use it as a function

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

    The function deposito() returns a float soon just show this float directly, thus:

    print("O seu saldo é:", saldo) #agora sem os ()
    
  • The montante it is not global all functions so it will not accumulate as it performs the various operations. To make it 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
    

Browser other questions tagged

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