What I have wrong in my if in python

Question:

I am doing this exercise, and it always throws me the last question (Today I rest). I don't understand what I'm doing wrong, surely something very simple (but, that's when the answer is found).

Create a program that asks from the keyboard if there is "chocolate" first and if there is "flour" after.

chocolate = 0
harina=0

chocolate = input("Hay Chocolate?")
harina = input("Hay Harina?")


if chocolate == True:
    if harina == True:
        print("Cocinamos tarta")
    else:
        print("Haremos bombones")
else:
    if harina == True:
        print("Hornearemos pan")
    else:
        print("Hoy descanso")

Print:
If both are true we will print "We baked a cake"
If only "chocolate" is true we will print "We will make chocolates"
If only "flour" is true we will print "We will bake bread"
If neither is true we will print "Today I rest"

Answer:

What is happening is that you are comparing what the user entered with a boolean. The input() function returns a string , and your condition will always return False if you compare its value to a boolean. Also, you don't need to define both variables with 0 at the beginning. To resolve this, you'll need to compare True as a string

chocolate = input("Hay Chocolate?").lower()
harina = input("Hay Harina?").lower()


if chocolate == "true":
    if harina == "true":
        print("Cocinamos tarta")
    else:
        print("Haremos bombones")
else:
    if harina == "true":
        print("Hornearemos pan")
    else:
        print("Hoy descanso")
Scroll to Top