I have created a program for recurring deposit. I have included a if-else statement with two elif. every time I run the code it is executing the if statement even if the condition passed in it is wrong. I would appreciate it if someone would help me out.
my code:
missing = input("What is missing in the question(MV, P or r): ")
if missing == "MV" or "mv":
P = int(input("Enter the monthly installment: "))
r = int(input("Enter the rate of interest: "))
n = int(input("Enter the time period in months: "))
MV = (int)(P*n+((P*n*(n+1)*r)/(2*12*100)))
print("MV = ", MV)
elif missing == "P" or "p":
MV = input("Enter the final amount: ")
r = input("Enter the rate of interest: ")
n = input("Enter the time period in months: ")
P = (int)(Mv*2400)/((2400*n)+(n*(n+1)*r))
print("P = ", P)
elif missing == "r" or "R":
MV = input("Enter the final amount: ")
P = input("Enter the month installment: ")
n = input("Enter the time period in months: ")
I = MV - P*n
r = (I*2400)/(P*n*(n+1))
print("r = ", r)
else:
print("wrong input")
>Solution :
Your problem is because if missing == "MV" or "mv": doesn’t do what you think it does. The right hand side condition is equivalent to if 'mv'. In Python non-empty strings are truthy meaning it will always evaluate to true. Instead you should do if missing == "MV" or missing == "mv":. Or even better you can convert missing to lowercase so you do not need a second condition: if missing.lower() == 'mv'