6 Replies - 693 Views - Last Post: 11 December 2015 - 04:57 AM Rate Topic: -----

#1 HelenaEngstrom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 05-October 15

Doubling the steps in for-loop

Posted 10 December 2015 - 01:00 PM

As the topic says, how do I write a for-loop which double the number of steps each iteration?

This is what I've tried:

for N in range(1,100,2):

    h=(b-a)/N
    s=0.5*f(a)+0.5*f(B)/>


But this only adds 2 to each new step. I would like to have an intervall in the range 1,2,4,8, etc.

Thankful for any help!
Is This A Good Question/Topic? 0
  • +

Replies To: Doubling the steps in for-loop

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Doubling the steps in for-loop

Posted 10 December 2015 - 01:38 PM

Something like:
for i in range(N):
    ex = 2 ** i  # will generate the values 1, 2, 4, 8, 16, ...
    h = (b - a) / ex

Was This Post Helpful? 0
  • +
  • -

#3 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Re: Doubling the steps in for-loop

Posted 10 December 2015 - 01:38 PM

This little snippet should prove helpful.
for i in range(10):
    print(2**i)



EDIT:
Ahhh he was just a moment faster than me!

This post has been edited by atraub: 10 December 2015 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 matttm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-September 15

Re: Doubling the steps in for-loop

Posted 10 December 2015 - 04:56 PM

View Postatraub, on 10 December 2015 - 01:38 PM, said:

This little snippet should prove helpful.
for i in range(10):
    print(2**i)



EDIT:
Ahhh he was just a moment faster than me!


I believe that he wants the actual loop's counter to double.

EDIT: I see said the blind man. Sry

This post has been edited by matttm: 10 December 2015 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 HelenaEngstrom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 05-October 15

Re: Doubling the steps in for-loop

Posted 11 December 2015 - 04:16 AM

Thank you for your answers, but it's still not working. I tried to run this code:

for N in range(40):
    ex=2*N

    h=(b-a)/(ex+1) # +1 bc otherwise I get zerodivisonerror
    s=0.5*f(a)+0.5*f(B)/>



but it still prints 2,4,6,8,.. :(
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: Doubling the steps in for-loop

Posted 11 December 2015 - 04:33 AM

That isn't the same code. Compare * and **. Look for ** here, the exponential operator.
Was This Post Helpful? 0
  • +
  • -

#7 HelenaEngstrom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 05-October 15

Re: Doubling the steps in for-loop

Posted 11 December 2015 - 04:57 AM

Now I see it, thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1