1

I need to create something that can multiply values no matter how many values there are. My idea is to use a list and then get the sum of the list with the sum function or get the first value of the list and set it as the total and then multiply the rest of the list by that total. Does anyone have a way to do it?

Here was my original idea:

total = 0
while True:
  number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1:  "))
  if number == 1:
    break
  else:
    total *= number
print(f"The total is: {total}")

however as you may have guessed it just automatically multiplies it to 0 which equals zero. I would also like the code to work for subtraction and division (Already got Addition working)

thanks!

Thanks to the comments I worked out that the way to do it is by changing the beginning total to a 1 when fixed it is this:

total = 1
while True:
  number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1:  "))
  if number == 1:
    break
  else:
    total *= number
print(f"The total is: {total}")
  • 2
    What is a number that you can multiply any number by and get the original number back? 0 is the number that does that for *addition*, but for multiplication it's... – hobbs May 13 '22 at 03:25
  • Firstly, `sum(number)` should be `total`. For the actual problem, 0 multiplied by any number is 0. That's math, not specific to Python. – wjandrea May 13 '22 at 03:29
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea May 13 '22 at 03:32
  • Related: [What's the function like sum() but for multiplication? product()?](/q/595374/4518341) – wjandrea May 13 '22 at 03:33

1 Answers1

1

Since you are multiplying, you must start with 1 cause anything multiplied by 0 is 0. And idk why you need sum()

total = 1
while True:
  number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1:  "))
  if number == 1:
    print(f"The total is: {total}")
    break
  else:
    total *= number
TheRavenSpectre
  • 367
  • 2
  • 11
  • Did you actually run this? The output's wrong. You need to print `total`, not `number`. – wjandrea May 13 '22 at 03:46
  • I sent the wrong code this is actual code: total = 0 while True: number = int(input("Enter a number and I’ll keep multiplying until you enter the number 1: ")) if number == 1: break else: total *= number print(f"The total is: {total}") – MIchael Dearing May 13 '22 at 05:24