3 Replies - 5434 Views - Last Post: 19 October 2012 - 07:56 AM Rate Topic: -----

#1 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

exponential function for loop

Posted 18 October 2012 - 07:02 PM

I need to use "for" loop instead of "while" loop


I want the number be like:

10
100
1000
10000
100000


for i in range(100, 100000+1, x)


what can I do for x, so the number will increment by power of 10

I know it will be easier to using the while loop, but I am not allow to use it ><
Is This A Good Question/Topic? 0
  • +

Replies To: exponential function for loop

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: exponential function for loop

Posted 18 October 2012 - 08:09 PM

well, rather than looping through tens of thousands of numbers, you could loop through exponents,

the first one could be 10**1, then 10**2, then 10**3. Notice that we now are starting at 1 and only need to increment by 1. This should be very easy to implement, but if you have trouble, show us what you have and we'll help you more from there :)
Was This Post Helpful? 0
  • +
  • -

#3 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Re: exponential function for loop

Posted 18 October 2012 - 08:50 PM

View Postatraub, on 18 October 2012 - 08:09 PM, said:

well, rather than looping through tens of thousands of numbers, you could loop through exponents,

the first one could be 10**1, then 10**2, then 10**3. Notice that we now are starting at 1 and only need to increment by 1. This should be very easy to implement, but if you have trouble, show us what you have and we'll help you more from there :)



Not getting the correct number at all....

I understand what you said, but HAVE trouble to implement into the code.
I tried to mess up with with range function for 10**1, 10**2, 10**10, by different order

for i in range(100, 10000000+1, 10**10)

above is one of the function I tried
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: exponential function for loop

Posted 19 October 2012 - 07:56 AM

ok, let's look at what that code really says.
for i in range(100, 10000000+1, 10**10)
for each value i, starting from 100, going all the way to 10000000+1, and counting by 10000000000

Well that doesn't seem very helpful, does it? instead of starting from 1 to 5, you're starting from 100, going to 10000001 and counting by 10000000000. That means that your loop will only iterate once because 100 + 10000000000 will be greater than 10000001.

Let's go over what a for loop really does using the interpreter. Imagine I want to create a for loop that will iterate 10 times, I would write:
>>> for i in range(10):
    print(i)
and the interpreter would output
0
1
2
3
4
5
6
7
8
9
Notice that it starts from 0 and goes to 9. That's good, but what if I wanted to do 1-10. I basically have two options... I can either do print(i+1) or I can adjust my for loop a little.
>>> for i in range(1,11):
    print(i)
and the interpreter would output
1
2
3
4
5
6
7
8
9
10
Notice that I gave range 2 parameters, a start value and an end value. In the code you used, you provided 3 values. When there's a third value, it's called a "step" or an "increment" i.e. instead of counting by 1's, we can count by some other value. Let's say I only wanted the odd numbers from 1 to 10. I would write:
>>> for i in range(1,11,2):
    print(i)
and the interpreter would output
1
3
5
7
9


That's a good, basic introduction to the for loop. In your case, you want to do 10**1, then 10**2, ... 10**n. etc... so instead of trying to make a for loop that creates all these values, why not write a for loop that creates the exponent and output 10**i? Using the overview I gave you, give that a shot and let us know how it goes.

This post has been edited by atraub: 19 October 2012 - 02:11 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1