2

I'm trying to write an if else statement in shorthand. An if else to check a value and increment if it has not reached a value, otherwise minus a set number.

As an if else this works, but trying to do this as a shorthand version and am stuck.

Is the issue you cant use -= or += in this context? Any help to understand would be appreciated.

Have tried day-=7 if day == 7 else day+=1 - but know this is wrong as the var is already referenced on the left.

If else works fine

day = 5
if day == 7:
    day-=7
else:
    day+=1

Trying to write this in shorthand and I get an error on the right day+=1 as this is clearly incorrect. Looking for some advice on how to increment the day value if day != 7

day = 5
day-=7 if day == 7 else +=1

The +=1 throws an error.

Expect day to be 6

kguckian
  • 70
  • 5
  • Look into modulo...[here](https://stackoverflow.com/questions/991027/how-to-calculate-a-mod-b-in-python) are some examples – Andrew Allen May 15 '19 at 00:07

4 Answers4

6
day += -7 if day == 7 else 1

The way you read this is "Add negative 7 to day if day == 7, otherwise, add 1 to day"

Dagorodir's original answer does not work, because it will subtract (day + 1) from the current value of day if day != 7. So using your example with the the starting value of 5 for day, the result of running the code from the other answer is -1.

Samantha Miller
  • 276
  • 1
  • 12
1

You are right in saying that you cannot use the assignment operators -= or += in some places in the context of a "conditional expression" in Python. My understanding is that the shorthand if-else is an expression, not a statement, as seen in your initial example. You cannot make assignments on the right hand side but instead specify the return value if false, and the return value (or assignment to the variable) if true on the left.

In your second attempt, on the right hand side, you have used the assignment operator without a variable. If you wanted the evaluation of the condition to take more complex values, you could assign to variables:

day = 5
ret_false = day + 1
ret_true = day - 7
ret_true if day == 7 else ret_false

Refer to Samantha's answer for more elegant solution: the += increment assignment operator is used for evaluation of either True or False in the expression - so -7 is necessary on the left, while += 1 is assigned otherwise. I repeat the inverse of the solution in her answer to illustrate the mechanics of the syntax; using the decremental -= operator on the left enforces its use on the right -

day -= 7 if day == 7 else -1 

Following the above logic, my original suggestion below uses the decremental -= assignment operator on the left; day-7 is returned if true, while day-(day+1) is returned if false. Thanks to Samantha for pointing this out.

Original

Try:

day -= 7 if day == 7 else day + 1

There are many questions regarding this: here and here, for example.

Dagorodir
  • 104
  • 1
  • 10
0

To me this is the most readable, using modulo arithmetic

day = 5
day = (day + 1) % 7
print(day)
# result 6

day = 6
day = (day + 1) % 7
print(day)
# result 0
Andrew Allen
  • 3,180
  • 2
  • 19
  • 46
0

I have two option for you:

day = day + (-7 if day == 7 else 1)

OR

day = day -7 if day == 7 else day + 1
mrSaraf
  • 103
  • 11