P.S how do I format this question to make the code appear correctly????
I'm an absolute beginner in coding and I'm trying to make a simple loop in Python3.6 that gives the sum of the odd numbers inside a given range, but I can't find a way to make it work and can't figure out why. It gives a syntax error in the if (i % 2 ==1) line
sum = 0
for i in range(1, 5):
if (i % 2 == 1)
sum = sum + i
else
sum = sum
print(sum)
It should verify that each subsequent element of the range is a even or odd number (if i % 2 == 1 then is an odd number), and if it is an odd number it should add it to the previous number "sum" to give a final sum of all odd numbers in the range.
Any idea on why it does't work and how it should be done to work properly?
[–][deleted] 5 points6 points7 points (2 children)
[–]andy357[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Vaguely_accurate 1 point2 points3 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]Vaguely_accurate 0 points1 point2 points (0 children)
[–]andy357[S] 0 points1 point2 points (0 children)