By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 448,562 Members | 1,298 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

# for and while loops

 P: n/a i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 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. " but im not quite grasping those functions.. please bear im mind i am an extreme newbie at this...thanks in advance Jun 28 '06 #1
Share this Question
6 Replies

 P: n/a hi #Exercise 1 : s=0 while 1: s+=input("Enter a num : ") if s>=100: print "The sum is greater than 100 : ",s break #Exercise 1 : s=0 for i in range(5): s+=input("Enter num #%d > "%(i+1)) print "The Sum is : " , s Jun 28 '06 #2

 P: n/a hi #Exercise 1 : s=0 while 1: s+=input("Enter a num : ") if s>=100: print "The sum is greater than 100 : ",s break #Exercise 2 : s=0 for i in range(5): s+=input("Enter num #%d > "%(i+1)) print "The Sum is : " , s Jun 28 '06 #3

 P: n/a ky*******@gmail.com schrieb: i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100. Write another program the hidden hint here is ... "read until" you can't know ahead how many numbers it will be, the pattern in this case is to use "while sum smaller then 100" sum = 0 while sum < 100: sum = sum + input("more numbers please: ") that reads 100 numbers from the user and prints out the sum. " here you know that you are going to read exactly 100 numbers sum = 0 for i in range(100): sum = sum + input("please number #%i: " % (i+1)) the only unclear point here is range(100) it generates a list with number [0,1,2 ... 99] and iterates through it one could write it like for i in [0,1,2,3,4]: do_something_with(i) but it gets tedious to write such a long list but im not quite grasping those functions.. please bear im mind i am an extreme newbie at this...thanks in advance hth, Daniel Jun 28 '06 #4

 P: n/a ky*******@gmail.com a écrit : i was wondering if anyone could point me to some good reading about the for and while loops There's not much to say. while : will execute as long as is True. for in : will execute for each in . ie : for letter in ["a", "b", "c"]: do_something_with(letter) is equivalent to letter = "a" do_something_with(letter) letter = "b" do_something_with(letter) letter = "c" do_something_with(letter) i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100. Since it's nearly impossible to predict how much iteration will be necessary for this condition to be satisfied[1], you want a while loop. The condition is 'the_sum >= 100' (starting with 'the_sum == 0'). The body of the loop is mainly : read a number in, add it to the_sum. [1] FWIW, we have 0 < number of iterations < +infinity, since nothing specifies that the user can not enter negative numbers !-) Write another program that reads 100 numbers from the user and prints out the sum. " Here you have a definite number of iterations, so it's a clear use case for a for loop, which will take care of the loop count by itself. Now since the for loop iterates over a sequence, you need such a sequence of 100 items. The canonical solution is the range(count) function, which will produce a sequence of integers. The body of the loop is exactly the same as in the previous case. but im not quite grasping those functions.. which 'functions' ? 'for .. in ..' and 'while ..' are statements (instructions), not functions. A functions is eval'd, and returns a value. A statement is executed, and has no value. HTH Jun 28 '06 #5

 P: n/a ky*******@gmail.com wrote: i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 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. " but im not quite grasping those functions.. please bear im mind i am an extreme newbie at this...thanks in advance while loops test a condition every time through the loop and keep running as long as it's true. for loops run through a finite (usually) set of things and do something "for" each thing. Check out one or more python tutorials such as http://docs.python.org/tut/tut.html (and especially this part http://docs.python.org/tut/node6.html), a quick search an google can turn up more. (Hint: you're probably gonna want to use a while loop for the first program and a for loop for the second.) Hope that helps, ~Simon Jun 28 '06 #6

 P: n/a Bayazee a écrit : hi #Exercise 1 : s=0 while 1: s+=input("Enter a num : ") if s>=100: print "The sum is greater than 100 : ",s break Why do you manually check the condition when the construct is meant to take care of it ? the_sum = 0 while the_sum < 100: try: sum += int(raw_input("please enter a num: ") except ValueError: pass #Exercise 1 : s=0 for i in range(5): Was supposed to be 100, not 5. s+=input("Enter num #%d > "%(i+1)) idem as above. Using input() is usually a bad idea: import sys s = input("please hack my machine: ") please hack my machine: sys.path.insert(0, '/path/to/malicious/stuff') sys.path[0] '/path/to/malicious/stuff' Jun 29 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.