|
1 |
| -operation = input(''' |
2 |
| -Please type in the math operation you would like to complete: |
3 |
| -1 for addition |
4 |
| -2 for subtraction |
5 |
| -3 for multiplication |
6 |
| -4 for division |
7 |
| -''') |
8 |
| - |
9 |
| -num = int(input('Enter your first number: ')) |
10 |
| -num2 = int(input('Enter your second number: ')) |
11 |
| - |
12 |
| -if operation == '1': |
13 |
| - print('{} + {} = '.format(num, num2)) |
14 |
| - print(num+num2) |
15 |
| - |
16 |
| -elif operation == '2': |
17 |
| - print('{} - {} = '.format(num, num2)) |
18 |
| - print(num-num2) |
19 |
| - |
20 |
| -elif operation == '3': |
21 |
| - print('{} * {} = '.format(num, num2)) |
22 |
| - print(num*num2) |
23 |
| - |
24 |
| -elif operation == '4': |
25 |
| - print('{} / {} = '.format(num, num2)) |
26 |
| - print(num / num2) |
27 |
| - |
28 |
| -else: |
29 |
| - print('Wrong Input! Please Try Again') |
| 1 | +operation = input(''' |
| 2 | +Please type in the math operation you would like to complete: |
| 3 | +1 for addition |
| 4 | +2 for subtraction |
| 5 | +3 for multiplication |
| 6 | +4 for division |
| 7 | +''') |
| 8 | + |
| 9 | +num = int(input('Enter your first number: ')) |
| 10 | +num2 = int(input('Enter your second number: ')) |
| 11 | + |
| 12 | +if operation == '1': |
| 13 | + print('{} + {} = '.format(num, num2)) |
| 14 | + print(num+num2) |
| 15 | + |
| 16 | +elif operation == '2': |
| 17 | + print('{} - {} = '.format(num, num2)) |
| 18 | + print(num-num2) |
| 19 | + |
| 20 | +elif operation == '3': |
| 21 | + print('{} * {} = '.format(num, num2)) |
| 22 | + print(num*num2) |
| 23 | + |
| 24 | +elif operation == '4': |
| 25 | + print('{} / {} = '.format(num, num2)) |
| 26 | + print(num / num2) |
| 27 | + |
| 28 | +else: |
| 29 | + print('Wrong Input! Please Try Again') |
| 30 | +======= |
| 31 | + |
| 32 | + |
| 33 | +"""The previous script becomes very boring for the user as he has to keep on typing just numbers and of wants to interact using |
| 34 | +simple english language he is unable to do so. Also the program terminates after one execution and if the user wants to keep on |
| 35 | +performing operations it becomes difficult. |
| 36 | +I have tried making a more simple and interactive calculator which allows the user to input any statement for performing the tasks. """ |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +def Calculator(num_1,num_2): |
| 42 | + #Executing the loop infinite times as we donot know how many times the user will want to run |
| 43 | + while(True): |
| 44 | + choice= input("Enter what you want to perform: ") |
| 45 | + print() |
| 46 | + |
| 47 | + #For Addition user can type any Sentence containing word related to it and will get the output but here we are checking only for the common words |
| 48 | + if ("addition" in choice) or ("add" in choice) or ("sum" in choice) or ("plus" in choice): |
| 49 | + sum = (num_1) + (num_2) |
| 50 | + print("Output....") |
| 51 | + print("Adding {} and {} results to {}".format(num_1,num_2,sum)) |
| 52 | + print() |
| 53 | + |
| 54 | + #For Subtraction user can type any Sentence containing word related to it and will get the output but here we are checking only for the common words |
| 55 | + elif ("subtraction" in choice) or ("minus" in choice) or ("difference" in choice) or ("subtract" in choice): |
| 56 | + if( num_1 > num_2 ): |
| 57 | + diff = (num_1) - (num_2) |
| 58 | + print("Output....") |
| 59 | + print("Subtracting {} from {} results to {}".format(num_2,num_1,diff)) |
| 60 | + print() |
| 61 | + elif( num_2 > num_1 ): |
| 62 | + diff = (num_2) - (num_1) |
| 63 | + print("Output....") |
| 64 | + print("Subtracting {} from {} results to {}".format(num_1,num_2,diff)) |
| 65 | + print() |
| 66 | + |
| 67 | + #For Multiplication user can type any Sentence cpntaining word related to it and will get the output but here we are checking only for the common words |
| 68 | + elif ("multiplication" in choice) or ("product" in choice) or ("multiply" in choice): |
| 69 | + if(num_1==0 or num_2==0): |
| 70 | + print("Output....") |
| 71 | + print("Multiplying {} and {} results to 0".format(num_1,num_2)) |
| 72 | + print() |
| 73 | + elif(num_1==1): |
| 74 | + print("Output....") |
| 75 | + print("Multiplying {} and {} results to {}".format(num_1,num_2,num_2)) |
| 76 | + print() |
| 77 | + elif(num_2==1): |
| 78 | + print("Output....") |
| 79 | + print("Multiplying {} and {} results to {}".format(num_1,num_2,num_1)) |
| 80 | + print() |
| 81 | + else: |
| 82 | + mul = (num_1) * (num_2) |
| 83 | + print("Output....") |
| 84 | + print("Multiplying {} and {} results to {}".format(num_1,num_2,mul)) |
| 85 | + print() |
| 86 | + |
| 87 | + #For Division user can type any Sentence cpntaining word related to it and will get the output but here we are checking only for the common words |
| 88 | + elif("division" in choice) or ("divide" in choice) or ("quotient" in choice): |
| 89 | + if( num_1 > num_2 ): |
| 90 | + if(num_2==0): |
| 91 | + print("Output....") |
| 92 | + print("Error: Please try with some other values!") |
| 93 | + |
| 94 | + elif(num_1==0): |
| 95 | + print("Output....") |
| 96 | + print("Dividing {} from {} results to 0".format(num_1,num_2)) |
| 97 | + print() |
| 98 | + else: |
| 99 | + div = (num_1) / (num_2) |
| 100 | + print("Output....") |
| 101 | + print("Dividing {} from {} results to {}".format(num_1,num_2,div)) |
| 102 | + print() |
| 103 | + elif(num_1==0 and num_2==0): |
| 104 | + print("Infinity!") |
| 105 | + print() |
| 106 | + elif( num_2 > num_1 ): |
| 107 | + if(num_1==0): |
| 108 | + print("Output....") |
| 109 | + print("Error: Please try with some other values!") |
| 110 | + print() |
| 111 | + |
| 112 | + elif(num_2==0): |
| 113 | + print("Output....") |
| 114 | + print("Dividing {} from {} results to 0".format(num_1,num_2)) |
| 115 | + print() |
| 116 | + else: |
| 117 | + div = (num_2) / (num_1) |
| 118 | + print("Output....") |
| 119 | + print("Dividing {} from {} results to {}".format(num_2,num_1,div)) |
| 120 | + print() |
| 121 | + |
| 122 | + #For exiting the loop user can type any Sentence containing word related to it and it will exit from the loop but here we are checking for the common words |
| 123 | + elif ("exit" in choice) or ("stop" in choice) or ("return" in choice): |
| 124 | + break |
| 125 | + |
| 126 | + else: |
| 127 | + print() |
| 128 | + print("Operation not found: Please try again!") |
| 129 | + print() |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + |
| 135 | + print() |
| 136 | + print(" THIS IS A BASIC USER FRIENDLY CALCULATOR! ") |
| 137 | + print() |
| 138 | + print("You can type a sentence and interact.") |
| 139 | + print() |
| 140 | + #inputting two numbers at a time using the split function |
| 141 | + num_1,num_2 = input("Enter two numbers: ").split() |
| 142 | + num1=float(num_1) |
| 143 | + num2=float(num_2) |
| 144 | + |
| 145 | + |
| 146 | + #printing both the numbers |
| 147 | + print("Number 1 is: ",num_1) |
| 148 | + print("Number 2 is: ",num_2) |
| 149 | + print() |
| 150 | + |
| 151 | + Calculator(num_1,num_2) |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + main() |
0 commit comments