CV88 CV88 - 1 month ago 12
Python Question

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)

Answer

The correct statement would be:

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

For reference, see Conditional Expressions.

Comments