Home > Back-end >  Don't know why the function is not returning the exact beverage type
Don't know why the function is not returning the exact beverage type

Time:11-07

def get_beverage_type(a_enabled: bool, b_enabled: bool) -> str:
    """
    Returns the beverage type defined by the switches.
    """
    if a_enabled == "y" == True and b_enabled == "n" == False:
        return "Juice, Orange"
    elif a_enabled == "n" == False and b_enabled == "y" == True:
        return "Milk, 2%"
    else:
        return "Coke, Diet" 
           
Switch_A = (input("Is switch A enabled? (y/n): "))
Switch_B = (input("Is switch B enabled? (y/n): "))

print(f"Result: {get_beverage_type(Switch_A, Switch_B)}")

Putting the values as Switch_A = y and and Switch_valueB = n , it still returns Result: Coke, Diet

CodePudding user response:

You appear to believe that a_enabled == "y" == True checks if a_enabled is one of those values.

That is not what it does. It is equivalent to a_enabled == "y" and "y" == True which of course is always False.

You probably meant a_enabled == "y" or a_enabled == True, for which there is no shorthand. However you can also use a_enabled in ("y", True) which is conceptually the same, even if it's technically not equivalent.

Edit: it's also possible that you meant (a_enabled == "y") == True, which is simply redundant.

CodePudding user response:

An expression like this:

b_enabled == "n" == False

is essentially saying b_enabled != "n", which I don't think is what you want (although if it is, you should just write it that way since it's simpler). Just remove the == True and == False stuff and your code does what I think you're trying to do:

def get_beverage_type(a_enabled: bool, b_enabled: bool) -> str:
    """
    Returns the beverage type defined by the switches.
    """
    if a_enabled == "y" and b_enabled == "n":
        return "Juice, Orange"
    elif a_enabled == "n" and b_enabled == "y":
        return "Milk, 2%"
    else:
        return "Coke, Diet"


print(get_beverage_type("y", "n"))  # Juice, Orange
print(get_beverage_type("n", "y"))  # Milk, 2%
print(get_beverage_type("n", "n"))  # Coke, Diet
print(get_beverage_type("y", "y"))  # Coke, Diet

CodePudding user response:

a_enabled = "y"

if a_enabled == "y" == True:
    print(1)

'''
it means 

a_enabled == 'y' and a_enabled == True . it is carzy.

so you should uese 

'''
if a_enabled == 'y' or a_enabled == True:
    print(1)
  • Related