+ 1

Is coding a working calculator possible? (without "import math")

Dear fellow learners, I programmed a calculator that can easily calculate with 3 inputs: two numbers and an arithmetic operator. Then I tried to add third number to the calculation, which will be calculated with the result of the first two numbers (ignoring the mathematical laws for now) The calculation won't work if i only use 3 inputs and also turn out an eof error with the var a if I use all 5 inputs. Please give me your opinion on how I can improve the calculator. Thanks a lot. This is my code: while True: x=float(input()) z=str(input()) y=float(input()) a=str(input()) b=float(input()) if z == "+": result=x+y elif z == "-": result=x-y elif z == "*": result=x*y elif z == "/": result=x/y elif z =="**": result=x**y else : result=x//y if a == "" or b == "" : print(x,z,y,"=",result) else : if a == "+": result+=b elif a == "-": result-=b elif a == "*": result*=b elif a == "/": result/=b elif a =="**": result**=b else : result//=b print(x,z,y,a,b,"=",result)

4th Sep 2018, 6:02 AM
Joseph.Atzinger
5 Réponses
+ 4
Joseph Atzinger you can remove that line 'import math' entirely and it will work happily on an equation like '5*(7/8)**4-8+(7/9)' but you will need it for 'math.log(7)*23'
4th Sep 2018, 6:16 AM
Louis
Louis - avatar
+ 4
try this Enter the entire equation in one line Eg. (4+9)*7-2 https://code.sololearn.com/ck9rtCKNCAdK/?ref=app
4th Sep 2018, 6:04 AM
Louis
Louis - avatar
+ 1
The code could be simplified using function lists instead of tests https://code.sololearn.com/cPsbrOHTCCgD/?ref=app
4th Sep 2018, 7:11 AM
VcC
VcC - avatar
+ 1
The code could be simplified using function lists instead of tests. See line 11-13 here https://code.sololearn.com/cPsbrOHTCCgD/?ref=app
4th Sep 2018, 7:11 AM
VcC
VcC - avatar
0
@Louis. Thank you, that is correct, however I wanted to see how it could work without "import math".
4th Sep 2018, 6:10 AM
Joseph.Atzinger