Search code examples
pythonpython-2.7maya

Python, if integer equals number


I'm an animator who is new to programming and I was wondering how to compare if a variable is equal to multiple numbers at once.

My code thus far:::

if intJoints==(3, 4, 6, 10):
    animValue=3
animValue=4

A formula for a 3D character I am animating always evaluates as "4" unless the character has 3, 4 , 6, or 10 joints in its spine.(Then it evaluates to "3" for some reason) Can I check all those values at once without making many "if" statement lines?

This syntax isn't working, it probably thinks I'm writing a tuple or something... Thank you for your help. Probably super basic to a pro coder, but it's actually hard to get a clear answer on basic stuff like this... Thanks!


Solution

  • You actually want to use the in keyword here.

    The correct syntax is

    if intJoints in (3, 4, 6, 10):
    

    You could do something like

    if intJoints == 3 or intJoints == 4 or intJoints == 6 or intJoints == 10
    

    but obviously using the in keyword is more concise and readable here.

    Your original syntax

    intJoints==(3, 4, 6, 10)
    

    tests to see whether intJoints is equal to the entire tuple. The only way this could possibly be true is if intJoints literally equals (3, 4, 6, 10).