在python中打印多个if语句?

时间:2017-12-30 06:10:24

标签: python if-statement

我正在开发一个简单的程序,要求输入3个输入,如果所有3个输入都匹配,则会打印一条欢迎信息。 (姓名缩写== YNH,年龄== 42,DOB == 1/27/74)。

此外,还会打印用户姓名(YoungNathanHeather)的长度以及打印三种不同字符串之一的输入(颜色)。

我遇到一个问题,即所有三个if语句都为真并打印字符串,即使输入蓝色,粉红色和黄色时变量(颜色)不同。

也许我错过了一些巨大的东西,嵌套了一堆if语句,但有没有人知道这里发生了什么?我希望输入“黄色”并只看到一个字符串打印。

#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')

2 个答案:

答案 0 :(得分:1)

当你说,

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

它被解释为,

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

如果color == 'blue'为真,则为真,如果'Blue'为真,则为。蓝色总是如此。与其他颜色相同

您可以执行以下操作:

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

将检查颜色列表中是否存在color。但是'BlUe','bLuE'等呢?这是一个更好的解决方案。

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

现在它匹配蓝色的任何大写!

接下来,请考虑以下部分:

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')

那是a lot of repetition,但也有很多不必要的缩进,这使得代码更难以阅读并且难以调试。为什么不更像:

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

这样做,你的代码将更容易阅读,也更容易编写!

答案 1 :(得分:1)

在Python中,这没有任何作用:if color == 'blue' or 'Blue',它总是真的!您必须将其更改为if color in ('blue', 'Blue')或更好:if color in {'blue', 'Blue'}

为什么什么都不做?因为当你以这种方式写你的时候,Python检查if color == 'blue',如果是,则执行;如果为False,则继续检查if 'Blue'。假设您的颜色为红色,则不符合标准的第一部分,因此Python会移至if 'Blue'。现在,这总是真的,因为根据Python:

bool(any string except empty string)为True,bool(any number except 0*)也是如此!因此,由于任何东西的布尔值都是True,因此if color == 'blue' or 'Blue'的if条件总是得到满足,无论你输入的是什么,除了空字符串或0之外。

此外,您可以将所有if加入一行:if Initials == 'YNH' and Age == '42' and DOB == '1/27/74'...

*嗯,更确切地说,0及其变体,例如0L,0j,0.0,0.00 ......

相关问题