In: Computer Science
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the menu above: The user will enter the number associated with the selected option. When the user chooses option 1, 2, 3, or 4, your program will prompt the user for two numbers that will be used in the operation. If the user enters 5, your program will quit The math operations will be as follows: o option 1, add the two numbers o option 2, subtract the second number from the first o option 3, multiply the two numbers o option 4, divide the first number by the second Print the result of the operation as a floating-point number to the nearest hundredth. For example, if the result is 4, print “4.00”, if the result is 36.45956, print “36.46” If the user attempts to divide by zero, display a message stating that division by zero is undefined, then display the menu again After the math operation has been performed and the result displayed, the program should once again display the menu. NOTE: The menu will be displayed continuously until the user selects 5 (quit) Should the user select an invalid number from the menu (i.e. outside the range 1 – 5), simply display the menu again 3. PYTHON PROGRAM Your program should follow the specification above. It should also include the following: Use comments on the first few lines to display your name, date, and a brief description of the program Include a brief comment before each major section of your code Use meaningful variable names, rather than short names that have no apparent meaning. For example, first_number or selectedOption are good variable names, whereas x and y are not.
This is for python and must run. Thank you!
PYTHON CODE:
loop=1
#while loop until loop==1
while(loop==1):
#menu option
print("Select Option number:\n(1) addtion\n(2) subtraction\n(3) multiplication\n(4) division\n(5) quit");
operation=int(input())
#if option is 5 then terminate loop
if operation==5:
print("terminated")
break
#if option is valid 1 to 5 range then ask for input of 2 values
if operation>=1 and operation<=5:
print("Enter first number:")
x=float(input())
print("Enter second number:")
y=float(input())
#if option is 1 perform addtion
if operation==1:
r=float(x+y)
r=round(r,2) #rounding to 2 decimal place
print("result:",r)
#if option is 2 perform subtraction
elif operation==2:
r=float(x-y)
r=round(r,2)
print("result: ",r)
#if option is 3 perform multiplication
elif operation==3:
r=float(x*y)
r=round(r,2)
print("result: ",r)
#if option is 4 perform division
elif operation==4:
r=float(x/y)
r=round(r,2)
print("result: ",r)
OUTPUT: