Python Program to Print the Sum and Product of All Numbers Input By User

In this example, we will write a simple program to take input from the user until he/she exits from the application and calculate the sum and product of all the numbers he/she entered. To better understand this example, make sure you have knowledge of the following tutorials:-

Python Program to Print the Sum and Product of All Numbers Input By User

sum = 0
product = 1

while True:
    user_input = int(input("Enter number: "))
    sum += user_input
    product *= user_input
    quit_confirm = input("Press q to exit / or Enter to continue: ")
    if quit_confirm == 'q':
        break

print("Sum = {}, Product = {}". format(sum, product))

The output of the above program is:-

Enter number: 5
Press q to exit / or Enter to continue:
Enter number: 7
Press q to exit / or Enter to continue:
Enter number: 3
Press q to exit / or Enter to continue: q
Sum = 15, Product = 105
Here while statement iterates indefinitely unless the user presses the q button. The sum and product are stored in two variables. The resulting display is done using the format operator. You can find more information about formatting output here, Python Input and Output.
SHARE Python Program to Print the Sum and Product of All Numbers Input By User

You may also like...

Leave a Reply

Your email address will not be published.

Share