In: Computer Science
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.
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: