Write a program that asks the user to continue to enter two numbers (at a time). For each pair of numbers entered, the program calculates the product of those two numbers, and then accumulate that product. For each pair of numbers entered, the program also prints the entered numbers, their product, and the accumulated products. The process will continue until the user enters two zeros. See the following screen snapshot for a sample running of the completed program. NOTE: Use a for loop.

Respuesta :

Answer:

def product_sum( num1, num2 ):

    accumulator = 0

    product = num1 * num2

    accumulator += product

    print ( " First number: {} \n, Second number: {} \n, \

           Product: {} \n, Accumulated product: {} ".format( num1, num2, product, \ accumulator))

num1 = 1

num2 = 0

for num1 == 0 and num2 ==0:

   for num1 == True and num2 == True:

   product_sum( num1, num2)

   num1 = int(input( "Enter first number: "))

   num2 = int( input(" Enter second number: " ))

Explanation:

The for loop in the python source code calls a function "product_sum" which multiplies two integer numbers and add the product to an existing variable.

The function prints out the current two numbers, the product and the accumulated products. The for loop continuously prompts for user input for the two numbers but stops when both numbers are zero.

def product_sum():

   accumulated_product = 0

   for _ in iter(int, 1):

       num1 = int(input("Enter a number: "))

       num2 = int(input("Enter a number: "))

       if num1 == 0 and num2 == 0:

           return

       product = num1 * num2

       accumulated_product += product

       print("The product of {} and {} is {}. The accumulated product is {}".format(num1, num2, product,

                                                                                    accumulated_product))

product_sum()

I first declared my product_sum function with no parameters. I then set the accumulated product to 0 and created an infinite for loop so we can run until both of the entered numbers are 0. I then ask the user to enter two numbers, checking if the two numbers are 0 and returning from the function if both numbers are 0. If the numbers are not 0, we get the product and then we add the product to accumulated product. We then print out a formatted string displaying the numbers, product, and accumulated product.