Python Forum
New Programmer/Need assistance with if statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New Programmer/Need assistance with if statements
#1
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
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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')))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New Programmer / Program ericvrocha 5 2,809 Oct-07-2019, 09:25 PM
Last Post: ericvrocha
  New programmer using GROK AmineLS 2 3,143 Sep-10-2019, 12:25 PM
Last Post: AmineLS

Forum Jump:

User Panel Messages

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