Python Forum

Full Version: ifelse is not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Length=float(input("Enter the length: "))
    Width=float(input("Enter the width"))
    Floor_Type=input("Enter the option Premium/P, Durable/D or Economy/E: ")
    #These are the inputs for the code, The first two are float as the decimal matters when dealing with length of wood. the floor type is a string as it is not a numerical value
    Area=Length*Width
    if Floor_Type == "P" or "Premium":
        Materials=Area*21.4
    elif Floor_Type == "D" or "Durable":
        Materials=Area*12.9
    elif Floor_Type == "Economy" and "E":
        Materials=Area*7.99
    else:
        print("Please enter a Floor type from the list provided.(Case Sensitive)
Whatever I entered at Floor_Type, always firs if statement is picked. When I enter 'D' or 'Durable' or 'Economy' or 'E' still the first calculation (Materials=Area*21.4) is performed. Any ide why is that?
Hello!
In Python a non-empty string is True:

In [1]: if "Premium":
   ...:     print('ok')
   ...:     
ok
Your first if statement is like this:
if (Floor_Type == "P") or "Premium":
Even if Floor_Type is not equal to 'P' and this evaluates as False the if statement will be true.

In [2]: if False or True:
   ...:     print('ok')
   ...:     
ok

In [3]: if False or "Premium":
   ...:     print('ok')
   ...:     
ok
See @Mekire's link