Computer Science
Write a menu-driven program to implement a simple calculator for two numbers given by the user.
Answer
while True:
print("Simple Calculator")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
choice = input("Enter choice (1/2/3/4/5): ")
if choice in ['1', '2', '3', '4', '5']:
if choice == '5':
print("Exiting the calculator.")
break
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
result = num1 + num2
print(num1, "+", num2, "=", result)
elif choice == '2':
result = num1 - num2
print(num1, "-", num2, "=", result)
elif choice == '3':
result = num1 * num2
print(num1, "*", num2, "=", result)
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(num1, "/", num2, "=", result)
else:
print("Error! Division by zero.")
else:
print("Invalid choice! Please select a valid option.")
Output
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 1
Enter first number: 234
Enter second number: 33
234.0 + 33.0 = 267.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 2
Enter first number: 34
Enter second number: 9
34.0 - 9.0 = 25.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 3
Enter first number: 2
Enter second number: 45
2.0 * 45.0 = 90.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 4
Enter first number: 452
Enter second number: 4
452.0 / 4.0 = 113.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 5
Exiting the calculator.
Related Questions
Meera wants to buy a desktop/laptop. She wants to use multitasking computer for her freelancing work which involves typing and for watching movies. Her brother will also use this computer for playing/creating games.
(i) Which of the following hardware has a suitable size to support this feature? Specify the reason.
- ROM
- RAM
- Storage
- All of these
(ii) Meera also wants to use this computer while traveling. What should her preference be — desktop or laptop?
(iii) If Meera has to run Python on her laptop, what should be the minimum RAM required to run the same?
(iv) What is the difference between RAM and storage? Do we need both in a computer?
Draw the structure of the components of a computer and briefly explain the following.
(a) Input Unit
(b) Output Unit
(c) Central Processing Unit
(d) Primary Memory
(e) Secondary Memory
Write a program that inputs a list, replicates it twice and then prints the sorted list in ascending and descending orders.
Write a program to create a dictionary with the roll number, name and marks of n students in a class, and display the names of students who have secured marks above 75.