|
| 1 | +def calc(choice, first_num, second_num): |
| 2 | +# Python does not have switch case |
| 3 | +# There are other ways to implement this, dictionary mapping for example, but I started off with this |
| 4 | + if(choice=='addition' or choice=='add'): |
| 5 | + return first_num+second_num |
| 6 | + elif (choice=='subtraction' or choice=='subtract'): |
| 7 | + return first_num-second_num |
| 8 | + elif(choice=='multiplication' or choice=='multiply'): |
| 9 | + return first_num*second_num |
| 10 | + elif (choice=='division' or choice=='divide'): |
| 11 | + return first_num/second_num |
| 12 | + |
| 13 | +print("This is the Basic Calculator Program") |
| 14 | +start=input("Do you wish to start? Type 1 for YES and 0 for NO: ") |
| 15 | +st=int(start) |
| 16 | +if(st!=1 and st!=0): |
| 17 | + print("I'm going to take that as a YES") |
| 18 | +while (st==1): |
| 19 | + num1=input("Enter the first number: ") |
| 20 | + first_num=int(num1) |
| 21 | + # I could use some thing like |
| 22 | + # Press the corresponing number to perform the operation |
| 23 | + # 1. Addition |
| 24 | + # 2. Subtraction |
| 25 | + # And so on, but I wanted to attempt taking a word input |
| 26 | + choice=input("Addition, subtraction, multiplication or division? ") |
| 27 | + choice=choice.lower() |
| 28 | + num2=input("Enter the second number: ") |
| 29 | + second_num=int(num2) |
| 30 | + answer=calc(first_num, choice, second_num) |
| 31 | + a=int(answer) |
| 32 | + print("The answer is "+str(a)) |
| 33 | + continue_=input("Do you want to continue? Press 1 for YES and 0 for NO: ") |
| 34 | + st=int(continue_) |
| 35 | + if(st!=1 and st!=0): |
| 36 | + print("I'm going to take that as a no...") |
| 37 | + |
| 38 | +print("Thank you for trying out the Bacic Calculator Program!") |
0 commit comments