### Re: [Tutor] Trouble with SUM()

```On 20/04/2019 21:51, Ju Bo wrote:
> Hi, I'm trying to write a program that uses a while loop to ask a user for
> multiple values then use the program to add all the values, however many
> there may be, then print the sum.

Your code very nearly does that, it just needs a slight tweak.

>  I'm having trouble with the sum()
> function.

You don't really need sum() here but you could use it if you
particularly want to. But in that case your understanding
of how it works needs adjusting.

First lets look at how to do it without sum()

> con = "y"
>
> while con == "y":
> AMT = float(input("What is the price of the item? \$"))
> con = input("Would you like to continue? [y/n]")
> price = float(sum(AMT + AMT))

You want to add AMT to price not add AMT to itself.

The way to do that is to write

price = price + AMT

or using a Python shortcut:

price += AMT

You don't need to convert to float since you

Incidentally Python convention says to use all uppercase names
for constant values (In this case it might be something like
the maximum number of items you are allowed to buy). So you
should really change AMT to amt. But that is just a style issue.

Now, how would we use sum() if we wanted to?
sum() works by adding all the values of a sequence such as
a list or tuple. So to use sum() we first need to create an
empty list of amounts. Each time round the loop we then
append AMT to that list. At the end of the loop we can use
sum to get the final price.

price = sum(amounts)

Notice there are no addition operations inside  the call
to sum(). sum() does all the addition for us internally.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

```

### Re: [Tutor] Trouble with SUM()

```On Sat, Apr 20, 2019 at 04:51:11PM -0400, Ju Bo wrote:

> con = "y"
> while con == "y":
> AMT = float(input("What is the price of the item? \$"))
> con = input("Would you like to continue? [y/n]")
> price = float(sum(AMT + AMT))

That's a good first attempt.

Rather than set the price to float(sum(AMT + AMT)), which doesn't work,
set the price to 0 before the loop starts. Then each time through the
loop, set the price to price + AMT:

price = price + AMT

--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

```

### Re: [Tutor] Trouble with SUM()

```
On 20Apr2019 16:51, Ju Bo  wrote:

Hi, I'm trying to write a program that uses a while loop to ask a user for
multiple values then use the program to add all the values, however many
there may be, then print the sum.  I'm having trouble with the sum()
function.  My code is below:

con = "y"

while con == "y":

AMT = float(input("What is the price of the item? \$"))
con = input("Would you like to continue? [y/n]")
price = float(sum(AMT + AMT))

We generally would also like to see the output of a run and your
explaination of what is wrong with that output, compared to the output
you wanted.

However, there's an obvious problem in your code above: the value of
price is computed entirely from the latest amount, with no reference to
any previous amounts.

Also, sum() does not take a single numeric vlue, it takes an iterable,
such as a list.

I suspect what you want to do is append AMT values to a list which is
set up as an empty list before the loop.
Then _after_ the loop, use sum() to add up all the number in the list
and print that.

For your reference, here is help(sum):

sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and
may reject non-numeric types.

Cheers,
Cameron Simpson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

```

### [Tutor] Trouble with SUM()

```Hi, I'm trying to write a program that uses a while loop to ask a user for
multiple values then use the program to add all the values, however many
there may be, then print the sum.  I'm having trouble with the sum()
function.  My code is below:

con = "y"

while con == "y":

AMT = float(input("What is the price of the item? \$"))
con = input("Would you like to continue? [y/n]")
price = float(sum(AMT + AMT))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

```