#1

I want to make a sum that is equal or smaller than 10. For that, I want to create a list and then sum all itens in that list. Here is my code:

``````n = []
s = 0
for i in range(len(n)):
for s <= 10:
n[i] = raw_input("Number: ")
s += n[i]
print s``````

However, I keep getting this error:

Oops, try again. Your code looks a bit off. Check out the examples in the previous exercises if you need help! Your code threw the following error: invalid syntax (python, line 4)

Apparently, there is a syntax error with `for s <= 10` but I don't see that. For me it's the way it should be.

#2

This will not work ...

``for s <= 10:``

The expression `s <= 10` evaluates to `True` or `False`. Those are `bool` objects, which are not iterable.

You could use a `while` loop containing an `if` block and a `break` statement instead.

You do not need a nested loop.

Be aware that `raw_input` returns a `str` object.

#3

Ok. I got to complete the lesson, but the code isn't working the way it should.

``````n = []
s = 0
for i in range(len(n)):
while s <= 10:
n[i] = int(raw_input("Number: "))
s += n[i]
else:
print "Your sum is higher than 10"
print s``````

On the console I get:

``````Your sum is higher than 10
0
None``````

1) I never get to write any numbers. It never prints `"Number: "`
2) How should I put an `if` block inside the `while` loop? From what I've seen, that's not needed.

#4

First, let's decide how the user should indicate that they are done entering numbers. Should the user enter an empty string to do that, or would you prefer that the user enter a `0` or a negative number?

#5

They should stop entering numbers when the sum excedes 10. How could I establish this limit?

#6

The problem, as you have defined it, is not very well suited for a `for` loop. You may wish to redefine the problem a little bit to satisfy the exercise specifications. It does call for a `for` loop.

#7

Hi again @chipsurfer59539,

Your original idea was a good one. It is just that the exercise asks for a `for` loop, and your idea is best satisfied by a `while` loop.

`for` loops are usually, though not always, best for situations where you have a completed sequence or `dict` that you would like to process item-by-item, or where you would like to perform some action a specific number of times.

`while` loops are usually, though not always, best for situations where you would like to repeat an action until a particular event occurs. Concerning your idea, the event would be that the total has exceeded `10`.

Below is an example solution for your idea that utilizes a `while` loop ...

``````nums = []
# loop until the total exceeds 10
while sum(nums) <= 10:
nums.append(int(raw_input("Enter an integer: ")))
else:
print("The sum has reached {:d}.".format(sum(nums)))

Output ...

``````Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
The sum has reached 15.
Your list is [1, 2, 3, 4, 5].``````

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.