I am fairly new to python.
I have been asked to create a calculator using only string commands, conversions between int/string/float etc.(if needed), and using functions is a requirement. while and for loops can also be used.
The program needs to take an input of the form x/y or x/y/z, where x y z are any positive or negative number. Where "/" can be replaced by addition multiplication and subtraction as well. And where any number of white spaces can exist between operands and operators. This is an idea of what I have so far.
I would have a unique definition for +,-,/, and *. I would create a function for what the user inputs. I would use ".lstrip" and ".rstrip" to get rid of white spaces.
Now what I am having trouble with is creating the input function. I am very new to functions and this is basically what I have. I know it isn't much to work with but I am really stuck on how to properly enter the function.
def multiplication(x,a,y,b,z):
if (a== "*"):
return x*y
if (b== "*"):
return y*z
def division(x,a,y,b,z):
if (a== "/"):
return x/y
if (b== "/"):
return y/z
def addition(x,a,y,b,z):
if (a== "+"):
return x+y
if (b== "+"):
return y+z
def subtraction(x,a,y,b,z):
if (a== "-"):
return x-y
if (b== "-"):
return y-z
def (x,y,z):
x=0
y=0
z=0
zxc=int(input()):# this is where I get stuck and I don't know how to implement x,y,z into the input.
All help is appreciated. If you are unsure of whether the code you provide is too intense for my needs, please ask before wasting your time for me, making code that I can't possibly use. I promise to reply ASAP.
Basically I am trying to find a way to split the inputted string AND THEN start calculations with it.
Here is a possible solution outline using regular expressions. Error checking left as exercise. If this isn't homework and you'd like to see the fleshed-out solution, view it here
I have an alternative to your code. The user can enter stuff like: 8*6/4-3+3 and this will still work. It also will not crash if a letter (d, a, s) is entered. Very compact.
Code (Python v3.3.0):
If you're making just a toy calculator,
eval()
accepts local and global variables, so you could use something like this:Here's a sample console session:
Note that
eval()
is not secure. If you want to make something serious, you will have to parse the expression.Also, since your expressions are simple, you could use a regex to validate the input before
eval
ing it:Since this looks like homework, I doubt the OP is allowed to use the typical ways to solve the problem. I think this is an exercise in input validation and string manipulation; followed by program flow and understanding function return values.
There are two things you need to do here:
For #1, we know that valid inputs are numbers (positive or negative integers), and they must be in the form of an expression. So this means, the minimum length of the input will be three (two numbers and a math symbol) and characters (strings) in the input are not valid.
This is our basic loop to get the user's input:
The
check_input
method will validate whatever the user entered is accurate based on our rules:After you have the correct input, the next step is to split the input into the various parts that you need to feed to your math functions. I'll leave that part up to you.
Assuming you have the correct string (that is, it is valid input for your program), you now need to split it into two parts.
So we know that we have a limited set of operators
+,-,/,*
, so one idea is to use thesplit()
method of strings. This works well:You would try splitting the string with all of your operators and then check the results. Note that splitting a string with a character that doesn't exist won't raise any errors, but you'll just the string back:
So one approach is - split the string on the operators, if the resulting list has length > 2, you know that the first member of the resulting list is the left hand side of the operator, and the second member of the list is whatever is on the right hand side.
This works fine with positive numbers, with negative numbers however:
Good news is we aren't the first ones to reach this problem. There is another way to evaluate equations, called the Polish notation (also called prefix notation). Here's the algorithm from the wikipedia page:
To get a normal expression (called infix) to the polish flavor, use the shunting yard algorithm, which is my favorite train-based algorithm in computer science.
Use shunting yard to convert your expression to Polish notation, then use the pseudo code to solve the equation. You can use lists as your "stack".
Keep in mind all your inputs are in strings, so make sure you convert them to integers when you are doing the actual math.