我试图解决/ 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循环。
答案 0 :(得分:1)
这是一个逻辑错误。在while循环中,将“或”替换为“and。”
while not((type(i) == int) and (type(i) == float))
原因是当值不是整数而不是浮点数时,您只想继续询问值的输入。否则,它将永远是真的,因为它只需要在“Or”语句中将一个测试设为真。