Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Average of 3 Numbers (included number rounding)

This Python code defines a function roundDouble() that rounds a given float value to a specified number of decimal points. The function calculates the power of 10 based on decimalPoints, multiplies the value by this power, rounds the result, and then divides by the power to achieve the desired precision. The code then calculates the average of three numbers a, b, and c, prints the average, and prints both the rounded integer and the rounded double to two decimal places.

Code Example

def roundDouble(value, decimalPoints):
    # Calculate the multiplier as 10 raised to the power of decimalPoints
    d = pow(10, decimalPoints)
    # Round the value after shifting the decimal point to the right and then reposition the decimal point
    return round(value * d) / d

# Initialize variables
a = 11.54
b = 7.73
c = 10.54

# Calculate the average of a, b, and c
avg = (a + b + c) / 3

# Print the average with details
print("Average of " + repr(a) + ", " + repr(b) + " and " + repr(c) + " is: " + repr(avg))

# Print the rounded values: nearest integer and to 2 decimal places
print("Rounded value:\n" + repr(round(avg)) + "\n" + repr(roundDouble(avg, 2)) + "\n")

Output

Average of 11.54, 7.73 and 10.54 is: 9.936666666666666
Rounded value:
10
9.94

Code Explanation

Defining the roundDouble Function

def roundDouble(value, decimalPoints):
    # Calculate the multiplier as 10 raised to the power of decimalPoints
    d = pow(10, decimalPoints)
    # Round the value after shifting the decimal point to the right and then reposition the decimal point
    return round(value * d) / d
  • Purpose: Rounds a floating-point number to a specified number of decimal places.
  • Parameters:
    • value: The number to be rounded.
    • decimalPoints: The number of decimal places to round to.
  • Calculations:
    • d = pow(10, decimalPoints): Computes the multiplier as 10decimalPoints10decimalPoints.
    • round(value * d) / d: Shifts the decimal point to the right by multiplying by d, rounds the value, then repositions the decimal point by dividing by d.

Initializing Variables

a = 11.54
b = 7.73
c = 10.54
  • Purpose: Initializes three floating-point variables a, b, and c with specific values.

Calculating the Average

avg = (a + b + c) / 3
  • Formula: Average=𝑎+𝑏+𝑐3Average=3a+b+c
  • Purpose: Computes the average of a, b, and c.

Printing the Average

print("Average of " + repr(a) + ", " + repr(b) + " and " + repr(c) + " is: " + repr(avg))
  • repr(a), repr(b), repr(c): Converts the values of a, b, and c to strings for printing.
  • Message: Outputs the average of the three numbers.

Printing the Rounded Values

print("Rounded value:\n" + repr(round(avg)) + "\n" + repr(roundDouble(avg, 2)) + "\n")
  • round(avg): Rounds the average to the nearest integer.
  • roundDouble(avg, 2): Rounds the average to 2 decimal places using the roundDouble function.
  • Message: Displays the rounded values, both to the nearest integer and to two decimal places.