0

I'm working on a simple program that asks for 3 inputs and if all 3 inputs match then a welcome message is printed. (Initials == YNH, Age == 42, DOB == 1/27/74).

Additionally, the length of the user's name (YoungNathanHeather) is printed as well as an input (color) that prints one of three different strings.

I am having a problem where all three if statements are true and printing their strings even though the variable (color) is different when I input blue, pink, and yellow.

Maybe I am missing something huge here with nesting a bunch of if statements, but does anyone have an idea whats going on here ? I expect to input "Yellow" and see only one string print.

#checks if inputted information matches criteria.
#if all inputted information matches variable criteria
#(Initials == YNH, Age == 42, DOB == 1/27/74), then
#prints a Welcome message for the user.
if Initials == 'YNH':
    if Age == '42':
        if DOB == '1/27/74':
            print('Welcome Young Nathan Heather')
            print('Did you know that your name has  ' +str((len('YoungNathanHeather'))) +' letters.')
            #Asks for a color and prints a response based on if the color is pink, yellow, or blue
            color = input("If you could describe your mood in a color, what would it be ?")
            if color == 'blue' or 'Blue':
                print('According to my research Master, Blue and Yellow together  makes green. Green seems to be equal to money.')
            elif color == 'pink' or 'Pink':
                print('According to my research Master, Pink attracts women or makes a human into a women. Whatever comes first.')
            elif color == 'yellow' or "Yellow":
                print('According to my research Master, Yellow indicates a "mellow" mood. Prolong this mood if feasible.')
            else:
                print("My apologies Master. My research is shallow and I am not yet familiar with that mood's \"color\"")



        else:
            print('Sorry, Wrong Credentials Entered')
    else:
        print('Sorry, Wrong Credentials Entered')

else:
    print('Sorry, Wrong Credentials Entered')
FRiEDRiCE
  • 1
  • 1
  • 2
  • 1
    `SIVABALANs-MBP:Desktop siva$ python test.py Welcome Young Nathan Heather Did you know that your name has 18 letters. If you could describe your mood in a color, what would it be ?yellow color is :yellow According to my research Master, Blue and Yellow together makes green. Green seems to be equal to money. SIVABALANs-MBP:Desktop siva$ ` what is the output that you are expecting ? – Siva Dec 30 '17 at 06:26

2 Answers2

1

When you say,

        if color == 'blue' or 'Blue':

it's interpreted as,

        if (color == 'blue') or ('Blue'):

which is true if color == 'blue' is true, or if 'Blue' is true. Blue is always true. Same with the other colors

You could do something like:

        if color in [ 'blue', 'Blue']:

Which will check whether color exists within the list of colors. But what about 'BlUe', 'bLuE', etc? Here's an even better solution.

        if color.lower() == "blue":

Now it matches any capitalization of blue!

Next, consider this part:

if Initials == 'YNH':
    if Age == '42':
        if DOB == '1/27/74':
            print('Welcome Young Nathan Heather')
            ... ... ...
        else:
            print('Sorry, Wrong Credentials Entered')
    else:
        print('Sorry, Wrong Credentials Entered')

else:
    print('Sorry, Wrong Credentials Entered')

That's a lot of repetition, but also a lot of unnecessary indentation, and that makes the code harder to read and harder to debug. Why not something more like:

if Initials == 'YNH' and  Age == '42' and DOB == '1/27/74':
    print('Welcome Young Nathan Heather')
    ... ... ...
else:
     print('Sorry, Wrong Credentials Entered')

Do it that way, and your code will be easier to read, and easier to write too!

erik258
  • 14,701
  • 2
  • 25
  • 31
1

In Python, this does nothing: if color == 'blue' or 'Blue', it is always True!. You must change it to if color in ('blue', 'Blue') or even better: if color in {'blue', 'Blue'}.

Why does it do nothing? Because when you write your if that way, Python checks if color == 'blue', if True, executes; if False, moves on to checking if 'Blue'. Say your color is red, then first part of the criteria is not met, hence Python moves on to if 'Blue'. Now, this is always True, because according to Python:

bool(any string except empty string) is True, and so is bool(any number except 0*)! Therefore, since Boolean value of anything is True, the if condition of if color == 'blue' or 'Blue' is always satisfied, regardless of whatever you feed into it other than empty string or 0.

Also, you can join all of your ifs in one line: if Initials == 'YNH' and Age == '42' and DOB == '1/27/74'...

*Well, to be more precise, 0 and its variants, such as 0L, 0j, 0.0, 0.00...

FatihAkici
  • 4,679
  • 2
  • 31
  • 48