Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ifelse is not working
#1
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?
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#3
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020