2 Replies - 1980 Views - Last Post: 08 October 2012 - 01:48 PM Rate Topic: -----

#1 patash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 12

understanding the for loop

Posted 08 October 2012 - 07:16 AM

Writing a program in python that finds the sum,average and product of the average and sum
for all numbers divisible by 7 . the user should provide both lower and upper limits.

and this is what i have so far
Lower =int(input("Enter the lower bound of your range:" ))
Upper =int(input("Enter the Upper bound of your range:" ))
nCount = 0
Sum = 0


for nCount in range (Lower,Upper+1):
n=int(nCount/7)


Sum = Sum + n

Average = (Sum)/nCount

Product = Sum * Average

print ("The numbers divisible by 7 in your range are:", n, end=" ")
print ("The sum of values divisible by 7 in your given range is: ", Sum)
print ("The Average of values divisible by 7 in your given range is: ", Average)
print ("The product of the sum and average of numbers divisible by 7 in your range is: ", Product)


This post has been edited by baavgai: 08 October 2012 - 07:30 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: understanding the for loop

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: understanding the for loop

Posted 08 October 2012 - 07:39 AM

For the numbers divisible by 7, you want a list. The most obvious way to do this would be to check each number for divisibility by 7 using the modulo operator:

divisible_by_seven = list()      # list of numbers
total_of_divisible_by_seven = 0  # accumulator for the total

if nCount % 7 == 0:
  # add the number to the list
  # add the number to the accumulator


Later on you'll learn other techniques for this, but you've gotta start somewhere.
Typically in python, you're thinking of a for more as iterating over a range rather than as controlling a fixed number of iterations of a single step. This is a little hard to get hold of if you're used to a language like C or Java, but what I mean is in C you'd do something like
for (int i = 0; i <10; i++)
{
  total += i;
}


where in python you'd do
for i in range(10):
  total += i



These do exactly the same thing, but the focus is subtly different: in the C example, you're paying attention to the counting from 0 through 9, while in python you're paying attention to ten items, which are the integers 0, 1, 2, etc.
This makes more sense when you think of a different example. In C, if you have an array of words, you have to iterate it by numbers - the same for loop, but now the numbers are indexes into an array (which itself contains arrays of chars). You're going to have to spend at least half of your attention just getting the details of the loop right, at least until you've done it so many times it becomes natural - and then you're really in trouble, because you can make careless mistakes!

In python, if you have a list of words, you'd do something like
for word in word_list:
  # do something with the word


You can do the same thing with anything that you conceive of as a collection of items. So you can see how the shift of attention makes a big difference!

Again, later on you'll see even better techniques, like list comprehensions, which will clarify things even more, but that'll make more sense when you understand simple for expressions.

This post has been edited by jon.kiparsky: 08 October 2012 - 01:50 PM

Was This Post Helpful? 2
  • +
  • -

#3 patash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 12

Re: understanding the for loop

Posted 08 October 2012 - 01:48 PM

Thanks a lot man.

Really helped at lot.


View Postjon.kiparsky, on 08 October 2012 - 07:39 AM, said:

For the numbers divisible by 7, you want a list. The most obvious way to do this would be to check each number for divisibility by 7 using the modulo operator:


divisible_by_seven = list() # list of numbers
total_of_divisible_by_seven = 0 # accumulator for the total

if nCount % 7 == 0:
  # add the number to the list

# add the number to the accumulator

Later on you'll learn other techniques for this, but you've gotta start somewhere.
Typically in python, you're thinking of a for more as iterating over a range rather than as controlling a fixed number of iterations of a single step. This is a little hard to get hold of if you're used to a language like C or Java, but what I mean is in C you'd do something like
for (int i = 0; i <10; i++)
{
  total += i;
}


where in python you'd do
for i in range(10):
  total += i



These do exactly the same thing, but the focus is subtly different: in the C example, you're paying attention to the counting from 0 through 9, while in python you're paying attention to ten items, which are the integers 0, 1, 2, etc.
This makes more sense when you think of a different example. In C, if you have an array of words, you have to iterate it by numbers - the same for loop, but now the numbers are indexes into an array (which itself contains arrays of chars). You're going to have to spend at least half of your attention just getting the details of the loop right, at least until you've done it so many times it becomes natural - and then you're really in trouble, because you can make careless mistakes!

In python, if you have a list of words, you'd do something like
for word in word_list:
  # do something with the word


You can do the same thing with anything that you conceive of as a collection of items. So you can see how the shift of attention makes a big difference!

Again, later on you'll see even better techniques, like list comprehensions, which will clarify things even more, but that'll make more sense when you understand simple for expressions.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1