mercredi 21 mars 2018

Can I put an in-list statement and an if statement in one line in Python? [duplicate]

This question already has an answer here:

I'm trying to condense my code a bit and put if-else statements on single lines. When I try and do this for an if-else statement that includes an in-list statement, I get an error.

temperature = 10 if 'hi' in ['hi','2'] else temperature = 1

  File "<ipython-input-2-af6c452397be>", line 1
    temperature = 10 if 'hi' in ['hi','2'] else temperature = 1
                 ^
SyntaxError: can't assign to conditional expression

Aucun commentaire:

Enregistrer un commentaire