0
#workday progress and back step in weekend

dayfactor = 0.01
total_progress = 1
for i in range(365):
    if i % 7 == 6 or 0:
        total_progress *= 1-dayfactor
    else:
        total_progress *= 1+dayfactor
print("the total progress in a year is {:.2f}".format(total_progress))

The result of this code is total_progress = 13.35 but when I change if i % 7 == 6 or 0 into if i % 7 in [6,0] which I suppose the code will show me the same result, but the total_progress changed to 4.63. Is there any difference between this two codes?

cucumber
  • 27
  • 4
  • 3
    `i % 7 == 6 or 0` is not correct; see [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – kaya3 Feb 11 '20 at 02:15
  • 1
    `i % 7 == 6 or 0` does not do what you think it does. It is equivalent to `0 or (i % 7 == 6)` and therefore also `i % 7 == 6`. – Klaus D. Feb 11 '20 at 02:17
  • Although it sounds correct when you say it allowed, what you're really saying is `if (i % 7 == 6) or (0)` First you evaluate `i % 7 == 6` and if that is false, you check if `0` is true (which is always false). This means it's the same as just having: `if i % 7 == 6` In the other example you explicitly check if the answer is a part of a list. It is more equivalent to `if (i % 7 == 6) or (i % 7 == 0)` – flakes Feb 11 '20 at 02:19
  • `if a == b or c:` is equivalent to `if (a == b) or c:` In other words, `c` is treated as a Boolean value, and its value is OR-ed with the result of the comparison `a == b`. Remember, Python is not an English language processor. – Tom Karzes Feb 11 '20 at 02:24

0 Answers0