2 Replies - 510 Views - Last Post: 25 January 2015 - 04:35 PM Rate Topic: -----

#1 skllmyh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-January 15

Question about loops

Posted 25 January 2015 - 12:18 PM

Hi everyone,

Im newbie in programming.

and im trying to code this. IMAGE

 i = 7 
while i<100:
    i = i+(1/i)
print(i)


is that correct?
Is This A Good Question/Topic? 0
  • +

Replies To: Question about loops

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Question about loops

Posted 25 January 2015 - 02:00 PM

Does it work?

Your loop will never end because you never change the value of i within the loop.

Once you get it to run, compare the result to what it should be. It's probably out by 7.. and a bit (what about i = 100?).
Was This Post Helpful? 0
  • +
  • -

#3 skllmyh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-January 15

Re: Question about loops

Posted 25 January 2015 - 04:35 PM

View Postandrewsw, on 25 January 2015 - 02:00 PM, said:

Does it work?

Your loop will never end because you never change the value of i within the loop.

Once you get it to run, compare the result to what it should be. It's probably out by 7.. and a bit (what about i = 100?).


Well,yes it never ended. Therefore needed to add a second variable.

 a = 0
i = 7
while i <=100:
    a = a + (1/i)
    i = i + 1
print(a)


and also for `for loop`

 a = 0
for i in range(7,101):
    a = a + (1/i)
print(a)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1