sola - 10 months ago 34

Python Question

I am a beginner and I wrote this code but how can I make a result as a sum of these integers..

`a=100`

b=200

for a in range (b):

if a%2==1:

print a

Answer

If you mean the sum of all the numbers `a < x < b`

, then this will do it.

```
a = 100
b = 200
total = 0
for i in range(a,b+1):
total += i
print total
```

In this case it will print `15150`

.

As per your question, if you want just odd numbers:

```
for i in range(a, b+1):
if i % 2 != 0:
total += 1
```

I know this isn't a 1 liner approach to the solution, but I wrote it out in this manner so that you could understand it better and more clearly. Also because it looks like you were taking an approach like this, and I wanted you to see how it would be done the way that you were trying to do it.

Source (Stackoverflow)