Python Forum

Full Version: New Programmer/Need assistance with if statements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Everyone---New programmer. I am trying to create a program that helps women locate good products for their hair type. I just got started and I am running into a few issues. If my code makes sense I am trying to display a specific response depending on the letter answer they give. Unfortunately my code keeps printing out all the answers at once, no matter which letter is selected. I have posted the code below and the errors that I am getting. I am almost positive there is an issue with my if statements:

print ("Let's help you choose you hair texture")
print ("")
Hair_Quiz = input("What is your hair color? ")
print ("")
if Hair_Quiz == "black" or " dark brown":
    
    print ("    A) Upper case S shape curl")
    print ("    B) Coarse Upper case S springy ringlets")
    print ("    C) Tight corkscrew like curls similiar to the size of a straw or pencil")
    print ("    D) Thick, coarse S shaped springs coils similiar to the size of a coffee straw")
    print ("    E) Tightly packed dense springy coils that have a definite curl shape")
    print ("    F) Densly packed coils simialir to the shape of a Z")
    print ("")

q1 = input("What kind of curls do you have? ")


if q1 == "A" or "a":
    print (" You have 3A textured hair")
if q1 == "B" or "b":
    print ("You have 3B textured hair")
if q1 == "C" or "c":
    print ("You have 3C textured hair")
if q1 == "D" or "d":
    print ("You have 4A textured hair")
if q1 == "E" or "e":
    print ("You have 4B textured hair")
if q1 == "F" or "f":
    print ("You have 4C textured hair")
What is happening when I run the module:

Output:
Let's help you choose you hair texture What is your hair color? black A) Upper case S shape curl B) Coarse Upper case S springy ringlets C) Tight corkscrew like curls similiar to the size of a straw or pencil D) Thick, coarse S shaped springs coils similiar to the size of a coffee straw E) Tightly packed dense springy coils that have a definite curl shape F) Densly packed coils simialir to the shape of a Z What kind of curls do you have? A You have 3A textured hair You have 3B textured hair You have 3C textured hair You have 4A textured hair You have 4B textured hair You have 4C textured hair
Went to link and I am still confused. I have searched Google and stack overflow for assistance and still cannot figure out why it won't work.
if q1 == "A" or "a" will always be evaluated as True, because any non-empty string (in this case "a") is evaluated as True, so it is same as if q1 == "A" or True:
it should be if q1 == "A" or q1 == "a":. Same apply to rest of your if statements.
Finally, there are better ways to do ot, not using number of if statements

hair = {'a':'3A textured hair', 'b': '3B textured hair', 'c':'3C textured hair', 
        'd':'4A textured hair', 'e': '4B textured hair', 'f':'4C textured hair'}

q1 = input("What kind of curls do you have? ")
print('You have {}'.format(hair.get(q1.lower(), 'unknown textured hair')))
Thank you!!!! I knew it was something small I was doing wrong and of course I knew there had to be an easier way but just trying to use what I've learned so far. Again, thanks for your help :)