|
1 | | -num1=int(input("eneter a digit")) |
2 | | -num2=int(input("eneter a another digit")) |
3 | | -# defination for operators |
4 | | - |
5 | | -#addition |
6 | | -def add(num1, num2): |
7 | | - return num1+num2 |
8 | | -#substraction |
9 | | -def subtract(num1, num2): |
10 | | - return num1-num2 |
11 | | -#multiply |
12 | | -def multiply(num1, num2): |
13 | | - return num1*num2 |
14 | | -#division |
15 | | -def divide(num1, num2): |
16 | | - return num1/num2 |
17 | | - |
18 | | -#command for operation |
19 | | -print("choose operation") |
20 | | -print("press 1 for add") |
21 | | -print("press 2 for subs") |
22 | | -print("press 3 for multiply") |
23 | | -print("press 4 for devision") |
24 | | - |
25 | | - |
26 | | - |
| 1 | +while True: |
| 2 | + # definition for operators |
| 3 | + |
| 4 | + #addition |
| 5 | + def add(num1, num2): |
| 6 | + return num1+num2 |
| 7 | + |
| 8 | + #substraction |
| 9 | + def subtract(num1, num2): |
| 10 | + return num1-num2 |
| 11 | + |
| 12 | + #multiplication |
| 13 | + def multiply(num1, num2): |
| 14 | + return num1*num2 |
| 15 | + |
| 16 | + #division |
| 17 | + def divide(num1, num2): |
| 18 | + try: |
| 19 | + return num1/num2 |
| 20 | + except ZeroDivisionError: |
| 21 | + print ("WARNING: Invalid division, cannot divide by zero") |
| 22 | + |
| 23 | + #exponent |
| 24 | + def exponent(num1, num2): |
| 25 | + return num1**num2 |
| 26 | + |
| 27 | + while True: |
| 28 | + try: |
| 29 | + num1=float(input("Enter a digit: ")) |
| 30 | + num2=float(input("Enter another digit: ")) |
| 31 | + break |
| 32 | + except ValueError: |
| 33 | + print("The input was not a valid digit") |
27 | 34 |
|
| 35 | + #command for operation |
| 36 | + print("Choose an operation") |
| 37 | + print("Press 1 for addition") |
| 38 | + print("Press 2 for substraction") |
| 39 | + print("Press 3 for multiplication") |
| 40 | + print("Press 4 for division") |
| 41 | + print("Press 5 for exponent") |
28 | 42 |
|
29 | | -while True: |
30 | 43 | # take input from the user |
31 | | - choice = input("Enter choice(1/2/3/4): ") |
| 44 | + choice = input("Enter choice(1/2/3/4/5): ") |
32 | 45 |
|
33 | | - if choice in ('1', '2', '3', '4'): |
| 46 | + if choice in ('1', '2', '3', '4', '5'): |
34 | 47 |
|
35 | 48 | if choice == '1': |
36 | 49 | print(num1, "+", num2, "=", add(num1, num2)) |
37 | 50 |
|
38 | | - |
39 | | - |
40 | 51 | elif choice == '2': |
41 | 52 | print(num1, "-", num2, "=", subtract(num1, num2)) |
42 | 53 |
|
43 | 54 | elif choice == '3': |
44 | 55 | print(num1, "*", num2, "=", multiply(num1, num2)) |
45 | 56 |
|
46 | | - |
47 | | - |
48 | | - |
49 | | - |
50 | 57 | elif choice == '4': |
51 | 58 | print(num1, "/", num2, "=", divide(num1, num2)) |
| 59 | + |
| 60 | + elif choice == '5': |
| 61 | + print(num1, "to the power of", num2, "=", exponent(num1, num2)) |
| 62 | + |
52 | 63 | # check if user wants another calculation |
53 | 64 | # break the while loop if answer is no |
54 | | - next_calculation = input("Let's do next calculation? (yes/no): ") |
| 65 | + next_calculation = input("Do you want to do another calculation? (yes/no): ") |
55 | 66 | if next_calculation == "no": |
56 | 67 | break |
57 | 68 |
|
58 | 69 | else: |
59 | | - print("Invalid Input") |
| 70 | + print("Invalid input") |
0 commit comments