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 Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
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.
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and division. This program prompts user to enter data in the following order: First data is the number, second data is an operator and the third data is the number. You are required to: Write a function for addition. Write a function for subtraction. Write a function for multiplication. Write a function for division. Write a main program which calls four functions based on the...
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.
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.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT