Write A Python Program To Obtain Three Numbers And Print Their Sum

Introduction

This Python program will obtain three numbers from the user and print their sum. The program will also validate the user input to ensure that all three numbers are valid integers.

Python program to obtain three numbers from the user and print their sum

Python
def obtain_three_numbers_from_user():
  """Obtains three numbers from the user and validates them.

  Returns:
    A list of three valid integers.
  """

  numbers = []
  for i in range(3):
    number = int(input("Enter number {}: ".format(i + 1)))
    if number < 0:
      raise ValueError("Number must be a non-negative integer.")
    numbers.append(number)
  return numbers

def print_sum_of_numbers(numbers):
  """Prints the sum of the given numbers.

  Args:
    numbers: A list of integers.
  """

  sum = 0
  for number in numbers:
    sum += number
  print("The sum of the numbers is {}.".format(sum))

# Obtain the three numbers from the user.
numbers = obtain_three_numbers_from_user()

# Print the sum of the numbers.
print_sum_of_numbers(numbers)

Output:

Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
The sum of the numbers is 60.

Extended example

The following Python program allows the user to specify how many numbers they want to input and print the sum of all the numbers.

Python
def obtain_numbers_from_user(number_of_numbers):
  """Obtains a list of numbers from the user and validates them.

  Args:
    number_of_numbers: The number of numbers to obtain from the user.

  Returns:
    A list of valid integers.
  """

  numbers = []
  for i in range(number_of_numbers):
    number = int(input("Enter number {}: ".format(i + 1)))
    if number < 0:
      raise ValueError("Number must be a non-negative integer.")
    numbers.append(number)
  return numbers

def print_sum_of_numbers(numbers):
  """Prints the sum of the given numbers.

  Args:
    numbers: A list of integers.
  """

  sum = 0
  for number in numbers:
    sum += number
  print("The sum of the numbers is {}.".format(sum))

# Get the number of numbers from the user.
number_of_numbers = int(input("Enter the number of numbers to sum: "))

# Obtain the numbers from the user.
numbers = obtain_numbers_from_user(number_of_numbers)

# Print the sum of the numbers.
print_sum_of_numbers(numbers)

Output:

Enter the number of numbers to sum: 5
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
The sum of the numbers is 150.

Applications of the Python program

The Python program to obtain three numbers from the user and print their sum can be used in a variety of applications, such as:

  • Calculating the total cost of a purchase.
  • Calculating the total number of items in a collection.
  • Calculating the total distance traveled.
  • Calculating the total number of calories consumed.
  • Calculating the total amount of money saved.

Conclusion

The Python program to obtain three numbers from the user and print their sum is a simple and versatile tool that can be used for a variety of tasks.

Post a Comment

0 Comments