Simple Interest 计算器中不可避免的 while 循环

Inescapable while loop in Simple Interest calculator

我正在尝试解决 /r/DailyProgrammer 挑战 2,简单 https://www.reddit.com/r/dailyprogrammer/comments/pjbj8/easy_challenge_2/ 我试图在 python 中使用函数和一系列嵌套的 if-elif-else 来做到这一点 这是我的代码:

def interest(initial, rate, years):
    if (type(initial) == int or type(initial) == float) and (type(rate) == int or type(rate) == float) and (type(years) == int or type(years) == float):
        return initial * rate * years
    else:
        return 'Incorrect input type'

def initialAmount(interest, rate, years):
    if (type(interest) == int or type(interest) == float) and (type(rate) == int or type(rate) == float) and (type(years) == int or type(years) == float):
        return interest/(rate * years)
    else:
        return 'Incorrect input type'

def rate(interest, initial, years):
    if (type(initial) == int or type(initial) == float) and (type(interest) == int or type(interest) == float) and (type(years) == int or type(years) == float):
        return interest/(initial * years)
    else:
        return 'Incorrect input type'

def years(interest, initial, rate):
    if (type(initial) == int or type(initial) == float) and (type(rate) == int or type(rate) == float) and (type(interest) == int or type(interest) == float):
        return interest/(initial * rate)
    else:
        return 'Incorrect input type'


I = raw_input("Do you know the amount after interest? If yes, enter it, if not enter 'n':  ")

print I

if I.lower == 'n':
    i = raw_input("Enter your initial amount:  ")
    r = raw_input("Enter your interest rate in decimals:  ")
    t = raw_input("Enter amount of years after initial deposit:  ")
    print "the initial amount is " + i
    print "the interest rate is " + r
    print "the amount of years since the initial deposit is " + t
    while not((type(i) == int) or (type(i) == float)):
        i = raw_input("Enter a valid initial amount:  ")
    while not((type(r) == int) or (type(r) == float)):
        r = raw_input("Enter a valid interest rate in decimals:  ")
    while not((type(t) == int) or (type(t) == float)):
        t = raw_input("Enter a valid amount of years after initial deposit:  ")
    print interest(i, r, t)
elif type(I) == int or type(I) == float:
    i = raw_input("Do you know the amount before interest? If yes, enter it, if not enter 'n':  ")
    if i.lower() == 'n':
        r = raw_input("Enter your interest rate in decimals:  ")
        t = raw_input("Enter amount of years after initial deposit:  ")
        print "the interest rate is " + r
        print "the amount of years since the initial deposit is " + t
        while not(type(r) == int or type(r) == float):
            r = raw_input("Enter a valid interest rate in decimals:  ")
        while not(type(t) == int or type(t) == float):
            t = raw_input("Enter a valid amount of years after initial deposit:  ")
        print initialAmount(I, r, t)
    elif type(i) == int or type(i) == float:
        r = raw_input("Do you know the interest rate? If yes, enter it in decimals, if not enter 'n':  ")
        if r.lower() == 'n':
            t = raw_input("Enter amount of years after initial deposit:  ")
            print "the amount of years since the initial deposit is " + t
            while not(type(t) == int or type(t) == float):
                t = raw_input("Enter a valid amount of years after initial deposit:  ")
            print rate(I, i, t)
        elif type(r) == int or type(r) == float:
            t = raw_input("Do you know the amount of years after the initial deposit? If yes, enter it in decimals, if not enter 'n':  ")
            if t.lower == 'n':
                print years(I, i, r)
            elif type(t) == int or type(t) == float:
                print "I don't know why you needed me then."
            else:
                while not (type(t) == int or type(t) == float):
                    t = raw_input("Enter a valid amount of years after initial deposit:  ")
        else:
            while not (type(r) == int or type(r) == float):
                r = raw_input("Enter a valid interest rate in decimals:  ")
    else:
        while not(type(i) == int or type(i) == float):
            i = raw_input("Enter a valid initial amount:  ")
else:
    while not(type(I) == int or type(I) == float):
        I = raw_input("Enter a valid final amount:  ")

问题是,一旦它要求您输入最终金额的值,无论您输入有效数字还是输入 'n',它总是让您陷入无限循环,要求您输入一个有效的最终金额。我想知道为什么它不进入if或elif而是直接进入不可避免的while循环

这是一个逻辑错误。在您的 while 循环中,将 "or" 替换为 "and."

while not((type(i) == int) and (type(i) == float))

原因是当值不是整数也不是浮点数时,您只想继续询问输入值。否则,它将永远为真,因为在 "Or" 语句中它只需要一个测试为真。