/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 ループに直接入る理由を知りたいです。