471,344 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

for loop vs while loop question.

Hi, i am extremely new to python and i've been following a tutorial about learning to programm with python. After reading through the tutorial and answering the questions given by it i findally came to a question which i can't figure out!
***Warning: Extremely noobish, i know this***
the question is:
Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100. Write another program that reads 100 numbers from the user and prints out the sum.

... i really got stuck here ... i would be happy if i just got the answer to this one so i could see it and start to understand and apply the solution myself.

If you do chose to respond and help me out : Thanks a lot!

(P.S i'm going on a holiday tomorrow (19.7.2007) so i will not respond for a long time ... still i'm sort of hoping i can get a reply really fast ...)
Jul 18 '07 #1
10 6071
bvdet
2,851 Expert Mod 2GB
Hi, i am extremely new to python and i've been following a tutorial about learning to programm with python. After reading through the tutorial and answering the questions given by it i findally came to a question which i can't figure out!
***Warning: Extremely noobish, i know this***
the question is:
Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100. Write another program that reads 100 numbers from the user and prints out the sum.

... i really got stuck here ... i would be happy if i just got the answer to this one so i could see it and start to understand and apply the solution myself.

If you do chose to respond and help me out : Thanks a lot!

(P.S i'm going on a holiday tomorrow (19.7.2007) so i will not respond for a long time ... still i'm sort of hoping i can get a reply really fast ...)
Here's the first one. The second one will be similar.
Expand|Select|Wrap|Line Numbers
  1. numsum = 0
  2. while numsum < 100:
  3.     numsum += int(raw_input('Enter a number'))
  4.  
  5. print 'The sum of the numbers you entered is %d.' % numsum
HTH :)
Jul 18 '07 #2
bartonc
6,596 Expert 4TB
Two (of many) ways to do the second one:
Expand|Select|Wrap|Line Numbers
  1. numsum = 0
  2. for i in range(100):
  3.     numsum += int(raw_input('Enter a number'))
  4.  
  5. print 'The sum of the numbers you entered is %d.' % numsum
or
Expand|Select|Wrap|Line Numbers
  1. numsum = 0
  2. i = 0
  3. while i < 100:
  4.     numsum += int(raw_input('Enter a number'))
  5.     i += 1
  6.  
  7. print 'The sum of the numbers you entered is %d.' % numsum
I need to give this thread a new name, so let us know when you have read it. Thanks.
Jul 19 '07 #3
ah ok love y'all i read it & thx
Aug 9 '07 #4
bartonc
6,596 Expert 4TB
ah ok love y'all i read it & thx
I'm glad that you were able to dig up your thread (I was worried that you'd be unable).

Now I can rename it.


Welcome back!
Aug 10 '07 #5
I'm glad that you were able to dig up your thread (I was worried that you'd be unable).

Now I can rename it.


Welcome back!
I know sorry, long holiday -_- (added it 2 favourites so dont think it would have been that much of a problem tho)
Aug 10 '07 #6
I have a question thats not exactly related to the original topic, but is sorta related to the new name of the topic :P

are there any differences in speed when executing a for loop or a while loop? It seems to me that for loops complete faster but I have no real way of testing it, I don't think. Was wondering if others experianced the same thing.
Aug 12 '07 #7
bartonc
6,596 Expert 4TB
I have a question thats not exactly related to the original topic, but is sorta related to the new name of the topic :P

are there any differences in speed when executing a for loop or a while loop? It seems to me that for loops complete faster but I have no real way of testing it, I don't think. Was wondering if others experianced the same thing.
The results are a bit surprising: (Bare in mind that the import actually takes up over half of the times posted) I would have expected the call to range() to be the bottleneck.
test3.py:
Expand|Select|Wrap|Line Numbers
  1. def forLoopTest(n):
  2.     for i in range(n):
  3.         pass
  4.  
  5. def whileLoopTest(n):
  6.     i = 0
  7.     while i < n:
  8.         i += 1
  9.  
TimeTestModule.py:
Expand|Select|Wrap|Line Numbers
  1. import timeit
  2.  
  3. test1ExecStr = \
  4. """from test3 import forLoopTest; forLoopTest(10)"""
  5.  
  6. t = timeit.Timer(test1ExecStr)
  7. print t.timeit()
  8.  
  9. # 14.056588477
  10.  
  11. test2ExecStr = \
  12. """from test3 import whileLoopTest; whileLoopTest(10)"""
  13.  
  14. t = timeit.Timer(test2ExecStr)
  15. print t.timeit()
  16.  
  17. # 13.6136176525
Aug 12 '07 #8
ilikepython
844 Expert 512MB
The results are a bit surprising: (Bare in mind that the import actually takes up over half of the times posted) I would have expected the call to range() to be the bottleneck.
test3.py:
Expand|Select|Wrap|Line Numbers
  1. def forLoopTest(n):
  2.     for i in range(n):
  3.         pass
  4.  
  5. def whileLoopTest(n):
  6.     i = 0
  7.     while i < n:
  8.         i += 1
  9.  
TimeTestModule.py:
Expand|Select|Wrap|Line Numbers
  1. import timeit
  2.  
  3. test1ExecStr = \
  4. """from test3 import forLoopTest; forLoopTest(10)"""
  5.  
  6. t = timeit.Timer(test1ExecStr)
  7. print t.timeit()
  8.  
  9. # 14.056588477
  10.  
  11. test2ExecStr = \
  12. """from test3 import whileLoopTest; whileLoopTest(10)"""
  13.  
  14. t = timeit.Timer(test2ExecStr)
  15. print t.timeit()
  16.  
  17. # 13.6136176525
When I run the same code with 50 iterations. The for loop is definately faster than the while loop. (6.9 - 7.1 vs. 10.2 - 10.5). Changing the range() call to xrange() makes the for loop even faster (6.0 - 6.2).
Aug 12 '07 #9
bartonc
6,596 Expert 4TB
<snip> Changing the range() call to xrange() makes the for loop even faster (6.0 - 6.2).
Great catch, my friend. I forgot about that one. Thanks.
Aug 12 '07 #10
Thanks for those, I *must* remember the timeit module.

At the time I thought it was just lag (I was using the for loops vs while loops in my irc bots, with 3 channels spamming it on purpose) but now it know for sure.

Thanks again,
Will.
Aug 13 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

33 posts views Thread by Arthur | last post: by
3 posts views Thread by zeroDoNotYeSpamtype | last post: by
3 posts views Thread by Gustavo Randich | last post: by
22 posts views Thread by Jan Richter | last post: by
3 posts views Thread by nina297 | last post: by
44 posts views Thread by James Watt | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.