How can i sum the values obtained in a foor loop?

# DOubt about for loop

**stetim94**#2

you could declare a variable total, set the value of total to zero

then add each value to total

**stetim94**#4

what do you mean with each sum? You can update `total`

variable, so that total contains each value you add to it

I don’t know how to follow these instructions:

“Create a variable called total and set it to zero.

Loop through the prices dictionary.

For each key in prices, multiply the number in prices by the number in stock. Print that value into the console and then add it to total.

Finally, outside your loop, print total.”

**stetim94**#7

such details would have been very useful in the details of original question

What do you have so far?

for key in prices and stock:

print prices[key] * stock[key]

soma =

sorry for not showing first i didn’t want to get an easy answer but now i am stuck

**stetim94**#9

it doesn’t say you should loop through stock dictionary? Given the keys are the same for both dictionaries, just looping through one of the two dictionaries is enough.

now we need to add `prices[key] * stock[key]`

to soma or total (whatever you prefer to name the variable)

did you declare `soma`

before the loop?

**textninja12677**#10

actually no. but i dont have to first add them among themselves like:

soma = prices[key] * stock[key]

soma2 = sum(soma)

or something like that?

**stetim94**#11

why would you use `sum()`

? this built-in function requires an iterable like a list

you should simply update `soma`

variable, by adding `prices[key] * stock[key]`

to current value stored in `soma`

how do we update a variable? here is a puzzle for you:

```
i = 5
# make i 10 by updating i
print i # should output
```

**textninja12677**#12

what do you mean when you say update?

Because my problem is that i don’t know how to sum all the elements

The puzzle you offered is great but does not help me to understand how can i sum the values beetween themselves

i am getting a list like this:

48

24

45

0

i want to sum these numbers]

and then print the result

(thank you so much for the attention!)

**stetim94**#13

well, if we update a variable we can a total/sum:

```
total = 0
example = [1,2,3]
for i in total:
total = total + i
```

by updating the variable, we get the total

**textninja12677**#15

total = 0

for key in prices and stock:

total = prices[key] * stock[key]

for key in total:

total = total + key

print total

this is what i have so far, was that what you meant?

(and thank you so much youre awesome for helping me)

**stetim94**#16

no, why would you? keys for both dictionaries are the same

the problem that you get a list of numbers:

```
48
24
45
0
```

is because you assign a new value to soma each time:

```
soma = prices[key] * stock[key]
```

if you would to update `soma`

by adding the value already stored in `soma`

to `soma`

, we would get the total, as i demonstrated here:

```
total = 0
example = [1,2,3]
for i in total:
total = total + i
print total # 6
```

did you figure out my challenge for you:

```
i = 5
# make i 10 by increasing i to 10
print i # should output
```

**textninja12677**#17

i = 5

for counting in i

i = i + 10

print i

is that it?

i am still haing trouble with the loop, because i am doing a for loop for prices, and then i have to do another for total?

**stetim94**#18

why are you so obsessed with the second loop? For the last time: you don’t need a second loop, the only purpose of the loop is to get use stock and price information, given the keys for both dictionaries are the same, one loop iterator which contains the keys is enough.

now here:

```
soma = prices[key] * stock[key]
```

we want to increase `soma`

(which contains your total) by `prices[key] * stock[key]`

, by adding this each time to `soma`

, we get a total (as i demonstrated with a simply list)

currently, you overwrite `soma`

with a new value instead of increasing `soma`

, this way, you will never get to the total

**textninja12677**#19

Look, this is my problem step by step, with the instructions:

.

Let’s determine how much money you would make if you sold all of your food.

Create a variable called total and set it to zero.

total = 0

Loop through the prices dictionary.

for key in prices:

For each key in prices, multiply the number in prices by the number in stock. Print that value into the console and then add it to total.

total = prices[key] * stock[key]

print total

total = total + key

Finally, outside your loop, print total.

print total

where is my mistake, i am sorry to bother so much but i cant see my mistake

**stetim94**#20

here:

```
total = total + key
```

you increase total, but you should this earlier by increasing total with `prices[key] * stock[key]`

to actual get a total.

doing `total = prices[key] * stock[key]`

will just re-assign `total`

, not increase it