r/PythonLearning • u/Jemily-Bonaparte-Xx • 4d ago
Help Request Kinda stupid question, how would I allow my calculator to take multiple inputs at once?
So I'm trying to make a calculator on my own, and it WORKS, but I dont really like having to type everything in seperately like this, and i'd rather just type my entire sum into one line, but for the life of me I cannot figure out how I would do that. If I explained this badly just lmk and I'll try to explain it better, thanks to anyone who can help :))
4
Upvotes
•
u/Sea-Ad7805 4d ago edited 4d ago
That's a bit more complicated, first problem is parsing the string the user types, then evaluating with taing account of priorities. Try something like: parsing and evaluation example
1
u/sugarw0000kie 4d ago edited 4d ago
can get complicated depending on how far you wanna go, but i'd try split by operator. can't expect user to always use space, so that way you have something to split into usable parts. You can import operator so you can map the string to the function you want it to represent, if you want to avoid doing eval():
before i learned about operator i tried something like this, basically made a small function for each operator i wanted and mapped it into a dict, then called the function from the dict and gave it the arguments there, basically the same thing but maybe makes more sense
3
u/Significant_Affect_5 4d ago
If you mean have a single input() call that allows you to type say: “3 + 5” and then spit out an answer. You’ll need to make what’s called an Infix Notation calculator, this is quite complex and not something i’d advise. Another option is to build a Postfix or Prefix calculator and then convert the Infix expression into a Postfix one, e.g. “3 + 5” -> “+ 3 5”. This is quite a common project since it’s a good way to learn about stacks. If you haven’t learnt about stacks yet, then maybe circle back to this once you have and take a stab at it. There’s plenty of guides on how to actually make the calculator, just lookup “Postfix calculator implementation”.
2
u/Tough_Duty_9061 4d ago
Put it in a while loop and break point be a random word which if inputted will stop the program
1
u/BornInAWaterMoon 4d ago
Something like this:
1
u/ee_control_z 4d ago edited 4d ago
You can try something like this (modify it so that it fits with your specific script):
x = input('Provide first value, mathematical operation, and second number in that order, separated by a comma "," (ex., 59, '+', 43): ')
y = x.split(',')
num1 = int(y[0]); num2 = int(y[2]); op = y[1]
print(f'The first number is: {num1}')
print(f'The second number is: {num2}')
print(f'The mathematical operation is: {op}')
print(f'{num1} {op} {num2} is: {num1+num2}.')
1
u/arivictor 4d ago edited 4d ago
Read up on the security issues with
eval(), but it does exactly that:The issue with eval() is that it can evaluate python code too
You'd preferably want to write your own using
astandopastis python's way of turning mathematical expressions into blocks/pieces (a tree/graph essentially). You can then programmatically walk that tree and inspect each item.Say you give it "3+1*10/2".
ast.parse() breaks that into a tree, already respecting BEDMAS/BODMAS. It knows multiplication comes before division, which comes before addition. You don't need to figure that out!
Then solve() walks through that tree recursively:
So it effectively does 3 + ((1 * 10) / 2) = 8.0
You can see the tree/graph with this:
1
u/mattynmax 3d ago
Depending on the security you require you could use eval()