Skip to content
Advertisement

Using conditional expressions and incrementing/decrementing a variable

How do I to put the if statement into a conditional expression and how do I increment/ decrement a variable?

num_users = 8
update_direction = 3

num_users = 
if update_direction ==3:
   num_users= num_users + 1
else:
   num_users= num_users - 1

print('New value is:', num_users)

Advertisement

Answer

The correct statement would be:

num_users = num_users + 1 if update_direction == 3 else num_users - 1

For reference, see Conditional Expressions.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement