Search code examples
pythonif-statementraw-input

Python if expression does not work correctly with raw input


This script does not progress passed the first if expression! If the user enters DD, or F, the script acts as like the if state was true.

choice = raw_input("Cup size for bra: D, DD, or F: ")
if choice == "D" or "d":
    band_length = raw_input("Please enter the bra band length for your D size breasts: ")
    D_Statistics(band_length)
elif choice == "DD" or "dd":
    band_length = raw_input("Please enter the bra band length for your DD size breasts: ")
    DD_statistics(band_length)
elif choice == "F" or "f":
    band_length = raw_input("Please enter the bra band length for your F size breasts: ")
    F_statistics(band_length)

Solution

  • Your if statements will always evaluate to True currently.

    if choice == "D" or "d" evaluates to True on either the value of choice equaling "D", or the value of literal "d" being True; and the second part is thus always True.

    Instead, use

    if choice in ("D", "d"):
        ...
    elif choice in ("DD", "dd"):
        ...
    if choice in ("F", "f"):
        ...