Question

In: Computer Science

Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...

Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average).

The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user.

You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s passed into the function. This function will print the calculated value.

You need to define a function named calculateAverage which takes no parameters. This function will ask the user how many numbers they wish to input. This function will use the number of times to run the program within a for loop in order to calculate the total and its average. This function will print the calculated average.

You need a main section that contains a while loop. The while loop will be used to allow the user to run the program until they enter a value which ends the loop. The main should prompt the user for the operation (+,-,*,/, or avg) they wish to perform. The main should evaluate the entered data using if statements. The main should call the necessary function to perform the calculation.

Solutions

Expert Solution

CODE:

def calculateAverage():
    total = 0
    x = int(input("how many numbers: "))
    for i in range(x):
        n = int(input("Enter number {0}: ".format(i + 1)))
        total += n
    print("Total: ", total)
    print("Average: ", total / x)


def performCalculation(operator):
    ans = 0
    a = int(input("Enter 1st number: "))
    b = int(input("Enter 2nd number: "))
    if operator == "+":
        ans = a + b
    elif operator == "-":
        ans = a - b
    elif operator == "*":
        ans = a * b
    elif operator == "/":
        ans = a / b
    print("Answer: ", ans)


if __name__ == '__main__':
    while True:
        print("1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Avg\\Total\n0.Exit\n")
        choice = int(input("Enter your choice: "))
        if choice==1:
            performCalculation("+")
        elif choice==2:
            performCalculation("-")
        elif choice == 3:
            performCalculation("*")
        elif choice == 4:
            performCalculation("/")
        elif choice == 5:
            calculateAverage()
        elif choice == 0:
            break

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.


Related Solutions

Write a Behavioral model VHDL code that implements an ALU that can perform addition, subtraction, multiplication,...
Write a Behavioral model VHDL code that implements an ALU that can perform addition, subtraction, multiplication, shift right, shift left, logical NAND, and logical NOR. Write a VHDL test bench to test the ALU with at least one test vector per operation.
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
Ahrithmetic calculation about scientific notation. Mixed unit and multiplication, addition, subtraction, division too. this is my...
Ahrithmetic calculation about scientific notation. Mixed unit and multiplication, addition, subtraction, division too. this is my question above, please help me I need it to study.
1) Perform the following addition and subtraction operations. For subtraction, negate the subtrahend (the second value)...
1) Perform the following addition and subtraction operations. For subtraction, negate the subtrahend (the second value) and add. For each operation, show the interpretation as both unsigned and signed operations. Indicate whether an unsigned or signed overflow has occurred that invalidates the result under that interpretation. Use an eight bit byte for all operations and for the signed interpretation, use two’s complement representation. Spaces are used in the binary values only for readability a. 1001 1111 + 0111 1000 b....
Problem: Perform following operations in binary using 8-bit addition/subtraction/multiplication. 1. −80 + 42 2. −99 −...
Problem: Perform following operations in binary using 8-bit addition/subtraction/multiplication. 1. −80 + 42 2. −99 − 20 3. 60 − 70 4. −59 × 3 5. 52×−1
What is the primary reason that addition and subtraction are considered more complex than multiplication and...
What is the primary reason that addition and subtraction are considered more complex than multiplication and division with floating-point representations? What are subnormal numbers, and how do subnormal numbers help reduce the impact of underflow?
Write VHDL code for ALU 32bit. ALU must perform addition and subtraction. You are not allowed...
Write VHDL code for ALU 32bit. ALU must perform addition and subtraction. You are not allowed to use other libraries. Only this libraries are allowed to use: use library ieee; use ieee.std_logic_1164.all; Please do it correctly and include the comments for me to fully understand. Thank you.
Write VHDL code for ALU 32bit. ALU must perform addition and subtraction. You are not allowed...
Write VHDL code for ALU 32bit. ALU must perform addition and subtraction. You are not allowed to use other libraries only this is allowed to use library ieee; use ieee.std_logic_1164.all; Please write the comments for me to fully understand. Thank you.
python please take it out from class and make it all functions, add subtraction and multiplication...
python please take it out from class and make it all functions, add subtraction and multiplication functions with it. than you so much for helping me out. import random image = 'w' class cal(): def __init__(self, a, b): self.a = a self.b = b def add(self): return self.a + self.b a = random.randint(0, 9) b = random.randint(0, 9) obj = cal(a, b) print("0. Exit") print("1. Add") choice = int(input("Enter choice: ")) cnt = 0 # To hold number of tries...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5?...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5? Write a program to perform the summation of 10 even number starting from 2? Write a program to perform the summation of 10 odd number starting from 2? Write a program to perform the summation of 10 number starting from 2 and increment is given by user? Write a program to combine all operations from 1 to 4 in a single program using ‘Switch’...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT