On 20Apr2019 16:51, Ju Bo <justinthetranspor...@gmail.com> 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 <c...@cskk.id.au>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to