Write a program that asks the user to input two numbers calculates the average outputs the result

Please log in or register to answer this question.

Facebook Twitter LinkedIn Email

In this post, we will learn how to find the average of two numbers using the Python Programming language.

Average is defined as a found by adding all the numbers in a set together and then dividing them by the quantity of numbers in the list.

  • Average = Total sum / Total no. of terms

We will use the following approaches to find the average of two numbers:

  1. Using Standard Method
  2. Using User-Input
  3. Using Function

So, without further ado, let’s begin this tutorial.

# Python Program to Find Average of Two Numbers # First Number a = 5 # Second Number b = 10 # Calculating average of two numbers avg = (a + b) / 2 # Display output print("Average of two numbers = %.2f" %avg)

Output

Average of two numbers = 7.50

Python Program to Find Average of Two Numbers With User Input

# Python Program to Find Average of Two Numbers With User Input # Asking for input num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) # Calculating average of two numbers avg = (num1 + num2) / 2 # Display output print("Average of two numbers = %.1f" %avg)

Output

Enter the first number: 18 Enter the second number: 24 Average of two numbers = 21.0

How Does This Program Work?

num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: "))

The user is asked to enter two integers. Here, input is taken with the help of the input() function. The float() function is used to convert the input to a floating data type.

# Calculating average of two numbers avg = (num1 + num2) / 2

Then, we find the average of two numbers using the average formula: Average = Total sum / Total no. of terms.

# Display output print("Average of two numbers = %.1f" %avg)

The average of two numbers is displayed on the screen using the print() function. We have used %.1f format specifier to limit the value of average to 1 decimal place.

Python Program to Find Average of Two Numbers Using Function

# Python Program to Find Average of Two Numbers Using Function def average(a, b): return (a + b)/2 # Asking for input num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) # Calling out custom function avg = average(num1, num2) # Display output print("Average of two numbers = %.2f" %avg)

Output

Enter the first number: 7 Enter the second number: 8 Average of two numbers = 7.50

Conclusion

I hope after going through this post, you understand how to find the average of two numbers using the Python Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Hello everyone, here we will learn a simple logic to find average on N numbers in python. This program takes max numbers from user and calculates the sum of all the numbers in a loop and the final obtained sum is divided by total number of inputs taken. That results as the average of N numbers.

Write a program that asks the user to input two numbers calculates the average outputs the result
Average on N numbers — programminginpython.com

The formula for it is Average = ( n1+n2+n3+.....) / N , where N is the total number of inputs and n1,n2,n3.. are the values of each input.

Task :

To find average of N numbers with max numbers and its values are given from user.

Approach :

  • Read an input integer for asking max numbers using input() or raw_input().
  • Loop N number of times for taking the value of each number using input() or raw_input(), where N is the value entered in first step.
  • In the loop, sum the value of each number entered.
  • Finally divide the obtained sum with N, where N is the value entered in first step.
  • The result obtained in the previous step is our average we wanted.
  • Print the result.

Program :

Output :

Write a program that asks the user to input two numbers calculates the average outputs the result
Average on N numbers — programminginpython.com
Write a program that asks the user to input two numbers calculates the average outputs the result
Average on N numbers — programminginpython.com

Last update on May 28 2022 13:01:22 (UTC/GMT +8 hours)

Write a Python program to calculate the sum and average of n integer numbers (input from the user). Input 0 to finish.

Pictorial Presentation:

Write a program that asks the user to input two numbers calculates the average outputs the result

Sample Solution-1:

Python Code:

print("Input some integers to calculate their sum and average. Input 0 to exit.") count = 0 sum = 0.0 number = 1 while number != 0: number = int(input("")) sum = sum + number count += 1 if count == 0: print("Input some numbers") else: print("Average and Sum of the above numbers are: ", sum / (count-1), sum)

Sample Output:

Input some integers to calculate their sum and average. Input 0 to exit . 15 16 12 0 Average and Sum of the above numbers are: 14.333333333333334 43.0

Flowchart :

Write a program that asks the user to input two numbers calculates the average outputs the result

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Calculates the average of two or more numbers.

Use sum() to sum all of the args provided, divide by len().

Python Code:

def average(*args): return sum(args, 0.0) / len(args) print(average(*[1, 2, 3, 4])) print(average(1, 2, 3))

Sample Output:

2.5 2.0

Flowchart :

Write a program that asks the user to input two numbers calculates the average outputs the result

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get next day of a given date.
Next: Write a Python program to create the multiplication table (from 1 to 10) of a number.

What is the difficulty level of this exercise?

How do I merge two dictionaries in a single expression in Python?

For dictionaries x and y, z becomes a shallowly merged dictionary with values from y replacing those from x.

  • In Python 3.5 or greater:
  • z = {**x, **y}
  • In Python 2, (or 3.4 or lower) write a function:
  • def merge_two_dicts(x, y): z = x.copy() # start with x's keys and values z.update(y) # modifies z with y's keys and values & returns None return z

    and now:

    z = merge_two_dicts(x, y)
  • In Python 3.9.0a4 or greater (final release date approx October 2020): PEP-584, discussed here, was implemented to further simplify this:
  • z = x | y # NOTE: 3.9+ ONLY

Explanation

Say you have two dicts and you want to merge them into a new dict without altering the original dicts:

x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4}

The desired result is to get a new dictionary (z) with the values merged, and the second dict's values overwriting those from the first.

>>> z {'a': 1, 'b': 3, 'c': 4}

A new syntax for this, proposed in PEP 448 and available as of Python 3.5, is

z = {**x, **y}

And it is indeed a single expression.

Note that we can merge in with literal notation as well:

z = {**x, 'foo': 1, 'bar': 2, **y}

and now:

>>> z {'a': 1, 'b': 3, 'foo': 1, 'bar': 2, 'c': 4}

Ref: https://bit.ly/2Y8QU8e