# understanding the for loop

Page 1 of 1

## 2 Replies - 4320 Views - Last Post: 08 October 2012 - 01:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294736&amp;s=7a511403ccc40145e34a63a741a413fb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 patash

Reputation: 0
• 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

• Pancakes!

Reputation: 9512
• Posts: 16,465
• 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

### #3 patash

Reputation: 0
• 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.

jon.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.

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}