A shorter version of the calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

A shorter version of the calculator

Hi, After learning functions with arguments, I have made a smaller version of this calculator... Would like to post here, maybe someone finds this helpful. This calculator have only two options to select... Calculate or Quit. It also takes only calculation symbols rather than typing the add, subtract etc. I know it's not a hot shot ;) but it's just a try of a newbie. Hope good suggestions would come for me as well. =============================== def calc(num1, num2, symb): if symb == "+": answer = str(num1+num2) print("The Answer is " + answer) elif symb == "-": answer = str(num1-num2) print("The Answer is " + answer) elif symb == "*": answer = str(num1*num2) print("The Answer is " + answer) elif symb == "/": answer = str(num1/num2) print("The Answer is " + answer) while True: print("Options:") print("Enter '1' to Calculate numbers") print("Enter '0' to Quit") user_input = input(": ") if user_input == "0": break else: num1 = float(input("Enter First Number...:")) symbol = input("Enter Symbol..") num2 = float(input("Enter Scond Number...:")) calc(num1, num2, symbol) ================================

29th Sep 2016, 1:15 PM
Syed Aliraza
Syed Aliraza - avatar
1 Answer
0
while True: a = input('Enter a calculation') #example 1 + 1 print(eval(a)) #eval is used to evaluate a string as code # and return the result of the code
2nd Nov 2016, 2:26 PM
Voinea Andrei
Voinea Andrei - avatar