python if statement evaluation with multiple values

后端 未结 2 1173
轻奢々
轻奢々 2021-01-23 01:14

I\'m not exactly sure why but when I execute this section of code nothing happens.

while (True) :

    choice = str(input(\"Do you want to draw a spirograph? (Y         


        
相关标签:
2条回答
  • 2021-01-23 01:51

    It won't work because the 'N' literal always evaluates to True within your if statement.

    Your if condition currently stands as if choice == 'n' or 'N' :, which is equivalent to if (choice == 'n') or ('N'), which will always evaluate to True irrespective of the value of variable choice, since the literal 'N' always evaluates to True.

    Instead, use one of the following

    • if choice == 'n' or choice == 'N' :
    • if choice in 'nN' :
    • if choice in ('n', 'N') :

    The same holds for your elif block as well. You can read more about Truth Value testing here.

    0 讨论(0)
  • 2021-01-23 01:55

    This expression doesn't do what you want:

    choice == 'n' or 'N'
    

    It is interpretted as this:

    (choice == 'n') or 'N'
    

    You might want to try this instead:

    choice in 'nN'
    
    0 讨论(0)
提交回复
热议问题