Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

minimallinux

  • 3 years ago

How does iterMul work anyone ? def iterMul(a, b): result = 0 while b > 0: result += a b -= 1 return result It returns the product but how ?

  • This Question is Closed
  1. msmithhnova
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The loop is based on b and there loops b many times. Inside the loop it simply adds a so say you have a of 3 and b of 4. If you add 3 for times you get 3+3+3+3 which is the same as 3*4.

  2. eparizi
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    as long as b is greater than 0 (while b > 0) it's adding the value of "a" to the local variable "result" (result += a) and also subtracting "b" by 1 (b -= 1), so be goes towards 0, and for each iteration another "a" is added to "result"! , we'll have "b" iterations (for each positive "b") and it makes b*a

  3. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy