Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

take a list that only takes integers and take the sum of all integers passed

im trying to take this list that only takes integers and take the sum of it. the error im getting is (

can't multiply sequence by non-int of type 'float') from:
 print  (float(sum(bottles*.1)))

Code:

bottles = [] 

while True:

    option = str(input('would you like to contiune? yes or quit '))

    if option == 'yes':
      bottles = [float(input("enter a number: ")) for _ in range(7)]
     #i can't multiply sequence by non-int of type 'float'
      print  (float(sum(bottles*.1)))

    if option == 'quit':
      quit("you get no payment")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t multiply a list by a float (you can multiply by an integer, but that just repeats the list, it doesn’t multiply the numbers in the list). Calculate the sum first, then multiply that. It’s equivalent by the distributive property of multiplication over addition (and you’ll probably get a more accurate result because there are fewer opportunities for floating-point round-off errors).

print(sum(bottles) * .1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading