0

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")
  • Does this help? [How to test multiple variables against a single value?](https://stackoverflow.com/a/15112149/14277722) – Michael Szczesny Dec 16 '21 at 15:39
  • 1
    Does this answer your question? [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – God-status Dec 16 '21 at 16:05

2 Answers2

3

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'

BTables
  • 4,413
  • 2
  • 11
  • 30
0

@BTables beat me to it, but there are several other ways in which you could improve this code.

  1. For one, you can mitigate using the or operator in the first place by using the upper() or lower() functions, which are native to python. What they do is convert a string to either all uppercase characters, or lowercase characters.

In this case, I'm assuming you're including both mv and MV as inputs because it allows for more flexibility when entering inputs, however you can simplify this process by only checking for one spelling, and then modifying the string with one of the aforementioned built-in functions.

So in your code you could implement this improved validation using something like this:

missing = input("What is missing in the question(MV, P or r): ")

if missing.lower() == "MV":
   # then perform tasks

This makes your code more legible and efficient, as the program doesn't have to run as many checks on the string before executing the if-statement. This isn't such a huge problem with something as simple as this program, but parameters must sometimes be checked dozens of times, and using dozens of or operators simply isn't practical.

  1. I would also suggest using functions to perform the mathematical tasks, as it provides more flexibility, as well as makes your code easier to read for others.

  2. This is less of a performance issue, so much as a usability feature, but your code should

    a) Be contained within try-statements to catch errors and

    b) I would suggest putting the input into an infinite loop, so that if a user misspells a word, they do not have to restart the program. Something as simple as

While 1: # you could also use `while True` here
    missing = input("...")
    if missing.lower() == "MV":
        do.things()

Just some friendly suggestions, feel free to to as you please, of course! :-)

God-status
  • 141
  • 2
  • 14