Я верю, что ваш код верен, а математика неверна. Вот три способа решения проблемы.
ваше решение:
lst = range(1,101)
total = 0
for x in lst:
if x % 2:
total -= x
else:
total += x
print(total)
50
сумма четных чисел плюс сумма нечетных чисел:
def sumForLoop(max):
positiveEven = sum(range(2,max+1,2))
negativeOdd = -sum(range(1,max+1, 2))
print(positiveEven + negativeOdd)
sumForLoop(100)
50
Формула для всего:
def sumFormula(max):
print(-1**100 *math.floor(max/2))
sumFormula(100)
50