Orkun Oz
Orkun Oz

Reputation: 45

How can I sum the result of number values?

Hi im a beginner in Python. and I've just a simple question for you but I can't figure it out anyway.

for i in range(7,10):
    function= (1/i)
    i+=1
    print(function)

then it prints

0.14285714285714285
0.125
0.1111111111111111

but firstly I want to sum these values and after that print. How can i?

Upvotes: 0

Views: 106

Answers (2)

rodrigocf
rodrigocf

Reputation: 2089

What about something like this:

total = 0

for i in range(7,10):
    function = 1/i
    total = total + function

print(total)

The idea is that every time the iteration is ran i increases by one, also since the range is a list ([7,8,9,10]), the first time i will be 7, then 8 and so on. The only printed value will be the total sumation at the end. Hope this helps.

Upvotes: 1

elyase
elyase

Reputation: 40963

Python is almost like natural language:

print(sum(1 / i for i in range(7, 10)))

Upvotes: 1

Related Questions