简体   繁体   中英

Function is repeating when I don't want it to

The input function of my code is repeating 3 times before going onto the next function. I have tried using while true and break and return None but none of that helps.

def welcome():
    print("Welcome to the Interest Loan Calculator")

def inp():
    loan = input("Enter loan amount: ")
    rate = input("Enter interest rate: ")
    return loan, rate


def conv():
    loan, rate=inp()
    if loan.endswith('K'):
        multiplier = 1000
        loan = loan[0:len(loan)-1]
    elif loan.endswith('M'):
        multiplier = 1000000
        loan = loan[0:len(loan)-1] 
    return int(float(loan) * multiplier)

def calc():
    loan = conv()
    print (loan)

def close():
print ("close placeholder")

def  main():
    welcome()
    inp()
    conv()
    calc()
    close()

if __name__ == "__main__":
    main()

When running the code it asks me to input both loan and rate 3 times before moving to the calc function.

If you don't want to call inp three times, then don't. You call it once directly from the main program; you call it a second time through the sequence main => conv => inp, and a third time through main => calc => conv => inp.

I think you need to draw out the call tree of your program, and then alter it to match the design you actually want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM