Question

In: Computer Science

Write a Python script that will be used as a calculator to allow simple arithmetic computation...

Write a Python script that will be used as a calculator to allow simple arithmetic computation of two input integer numbers.

Requirements:

a. The script should first allow the user to select if they want to add, subtract, multiply or divide two integer digits.

b. The script should pass the user inputs to a function using arguments for computation and the results return to the main part of the script.

c. The return value will be tested to determine if it is a positive number or a negative number.

Solutions

Expert Solution

CODE:

# function to get choice from user
def choice():
    ch = int(input("Please choose (1 - Add, 2 - Subtract, 3 - Multiply, 4- Divide) ? "))

    # if choice is wrong then ask user to re-enter
    while (ch < 1 or ch > 4):
        ch = input("Wrong choice. Please choose (1 - Add, 2 - Subtract, 3 - Multiply, 4- Divide) ? ")

    # return choice
    return ch

# function to compute result
def compute(num1, num2, choice):

    # for addition
    if choice == 1:
        return num1 + num2
    # for subtraction
    elif choice == 2:
        return num1 - num2
    #for multiplication
    elif choice == 3:
        return num1 * num2
    # for division
    else:
        return num1 / num2

if __name__ == '__main__':

    # get choice
    ch = choice()
    # get input numbers from user
    num1 = float(input("Enter number 1: "))
    num2 = float(input("Enter number 2: "))
    # get result
    result = compute(num1,num2,ch)

    # check if result is negative or positive
    if result >= 0:
        print("The result is positive.")
    else:
        print("The result is negative.")

    # display the result upto 2 decimal places
    print("The result is {:.2f}".format(result))

OUTPUT:

Scenario 1:

Scenario 2:

Scenario 3:


Related Solutions

write a script named compute.sh that is used to do simple arithmetic for the user. There...
write a script named compute.sh that is used to do simple arithmetic for the user. There should be no command line arguments. Instead, all values from the user should be prompted for and read in to the script using the read command. Specifically, you need to ask the user for two integers and an operation string. The operation should be "add", "sub", "mul", "div", or "exp", for addition, subtraction, multiplication, division, and exponentiation, respectively. Your script should take the two...
Write a script that represents a simple command line calculator. It will accept two numbers from...
Write a script that represents a simple command line calculator. It will accept two numbers from command line, and then display sum, difference and product of these two numbers. Provide script code and screenshot of output. Using Linux Ubuntu bash
Write a python script to solve the 4-queens problem using. The code should allow for random...
Write a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting. "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Hill Climbing (your choice of variant)
Write and Compile a python script to solve the 4-queens problem using. The code should allow...
Write and Compile a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting using numpy "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Arc consistency
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
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...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
Make a python calculator which deals with arithmetic include plus, minus, divide, and multiply as well...
Make a python calculator which deals with arithmetic include plus, minus, divide, and multiply as well as explaining each answer and use if statement using sign operator.
write a python script for rock scissors paper game
write a python script for rock scissors paper game
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT