The last exercise of chapter 5 continues the exercise made in the previous chapter. In…
- User
The last exercise of chapter 5 continues the exercise made in the previous chapter. In this exercise, expand the calculator so that the user can select what kind of calculation is done. If the user chooses 1, the calculator does addition as earlier. If 2, the calculator does substraction, if 3, it does multiplication, if 4, division.
Also add the instructions for the user to know what to do as shown in the example below. Also, if the user selects anything else besides 1-4, the program prints "Selection was not correct.". When working correctly, the progam looks like the following:
Calculator Give the first number: 100 Give the second number: 25 (1) + (2) - (3) * (4) / Please select something (1-4): 3 The result is: 2500
If the user selects something else besides 1-4, it prints the following:
Calculator Give the first number: 320 Give the second number: 225 (1) + (2) - (3) * (4) / Please select something (1-4): 100 Selection was not correct.
Errors such as the user giving input which is not a number, or division by 0, can be ignored at this point.
Example output: Calculator Give the first number: 1 Give the second number: 2 (1) + (2) - (3) * (4) / Please select something (1-4): 200 Selection was not correct. The verification of program output does not account for whitespace and is not case-sensitive (the least strict comparison level)
- Assistant
def calculator(): print("Calculator") first_num = float(input("Give the first number: ")) second_num = float(input("Give the second number: "))
print("(1) +") print("(2) -") print("(3) *") print("(4) /") selection = int(input("Please select something (1-4): ")) if selection == 1: result = first_num + second_num print("The result is:", result) elif selection == 2: result = first_num - second_num print("The result is:", result) elif selection == 3: result = first_num * second_num print("The result is:", result) elif selection == 4: # Here, you can add a condition to check for a division by 0. result = first_num / second_num print("The result is:", result) else: print("Selection was not correct.")calculator()
- User
make sure that the 4th run of the program ads .0 to the output if 4 is chosen: The last exercise of chapter 5 continues the exercise made in the previous chapter. In this exercise, expand the calculator so that the user can select what kind of calculation is done. If the user chooses 1, the calculator does addition as earlier. If 2, the calculator does substraction, if 3, it does multiplication, if 4, division.
Also add the instructions for the user to know what to do as shown in the example below. Also, if the user selects anything else besides 1-4, the program prints "Selection was not correct.". When working correctly, the progam looks like the following:
Calculator Give the first number: 100 Give the second number: 25 (1) + (2) - (3) * (4) / Please select something (1-4): 3 The result is: 2500
If the user selects something else besides 1-4, it prints the following:
Calculator Give the first number: 320 Give the second number: 225 (1) + (2) - (3) * (4) / Please select something (1-4): 100 Selection was not correct.
Errors such as the user giving input which is not a number, or division by 0, can be ignored at this point.
Example output: Calculator Give the first number: 1 Give the second number: 2 (1) + (2) - (3) * (4) / Please select something (1-4): 200 Selection was not correct. The verification of program output does not account for whitespace and is not case-sensitive (the least strict comparison level)
- Assistant
def calculator(): print("Calculator") first_num = float(input("Give the first number: ")) second_num = float(input("Give the second number: "))
print("(1) +") print("(2) -") print("(3) *") print("(4) /") selection = int(input("Please select something (1-4): ")) if selection == 1: result = first_num + second_num print("The result is: {:.1f}".format(result)) elif selection == 2: result = first_num - second_num print("The result is: {:.1f}".format(result)) elif selection == 3: result = first_num * second_num print("The result is: {:.1f}".format(result)) elif selection == 4: # Here, you can add a condition to check for a division by 0. result = first_num / second_num print("The result is: {:.1f}".format(result)) else: print("Selection was not correct.")calculator()