Python creating a calculator - python

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.

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:
Figure out what would be valid inputs to your program.
Keep prompting the user till he or she enters input that is valid for your program.
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:
expression = raw_input('Please enter the expression: ')
expression_result = check_input(expression)
while not expression_result:
print 'You did not enter a valid expression'
expression = raw_input('Please enter the expression: ')
expression_result = check_input(expression)
The check_input method will validate whatever the user entered is accurate based on our rules:
def check_input(input_string):
# Check the basics
if len(input_string) < 3:
return False
# Check if we are getting passed correct characters
for character in input_string:
if character not in '1234567890' or character not in '/*+-':
return False
# Things like /23 are not valid
if input_string[0] in '/*+':
return False
return input_string
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.
The operator (the math symbol)
The operands (the numbers surrounding the math symbol)
So we know that we have a limited set of operators +,-,/,*, so one idea is to use the split() method of strings. This works well:
>>> s = '4+5'
>>> s.split('+')
['4', '5']
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:
>>> s = '4+5'
>>> s.split('/')
['4+5']
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:
>>> s = '-4+3'
>>> s.split('-')
['', '4+3']
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:
Scan the given prefix expression from right to left
for each symbol
{
if operand then
push onto stack
if operator then
{
operand1=pop stack
operand2=pop stack
compute operand1 operator operand2
push result onto stack
}
}
return top of stack as result
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.

If you're making just a toy calculator, eval() accepts local and global variables, so you could use something like this:
def calculate(x=0, y=0, z=0):
expression = raw_input('Enter an expression: ')
return eval(expression, None, locals())
Here's a sample console session:
>>> calculate()
Enter an expression: x + 5 - y
5
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 evaling it:
def validate(expression):
operator = r'\s*[+\-/*]\s*'
return bool(re.match(r'^\s*(?:x{o}y|x{o}y{o}z)$'.format(o=operator), expression))

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
import re
# input is a list of tokens (token is a number or operator)
tokens = raw_input()
# remove whitespace
tokens = re.sub('\s+', '', tokens)
# split by addition/subtraction operators
tokens = re.split('(-|\+)', tokens)
# takes in a string of numbers, *s, and /s. returns the result
def solve_term(tokens):
tokens = re.split('(/|\*)', tokens)
ret = float(tokens[0])
for op, num in <FILL THIS IN>:
# <apply the operation 'op' to the number 'num'>
return ret
# initialize final result to the first term's value
result = solve_term(tokens[0])
# calculate the final result by adding/subtracting terms
for op, num in <FILL THIS IN>:
result += solve_term(num) * (1 if op == '+' else -1)
print result

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):
valid_chars = "0123456789-+/* \n";
while True:
x = "x="
y = input(" >> ")
x += y
if False in [c in valid_chars for c in y]:
print("WARNING: Invalid Equation");
continue;
if(y == "end"):
break
exec(x)
print(x)

Related

How do I let the program understand the input

I'm making a calculator that can add, subtract, multiply, and divide algebraic terms. I've already made a class that can "make" the algebraic terms, and as of now I want the computer to ask me for an algebraic expression, read it, then register it as one. (I'm really not sure about the correct wording, forgive me, I am new to coding.)
# Make a calculator that adds, subtracts, multiplies, and divides algebraic expressions.
# An algebraic expression has: a coefficient, a variable, and a power
# Class that makes the algebraic expression
class ExpressionMaker:
# Defines the coefficient, variable, and power of the algebraic term.
def __init__(self, coefficient, variable, power):
self.coefficient = coefficient
self.variable = variable
self.power = power
# Types down/returns/defines? the whole algebraic expression as one term.
def term(self):
return "{}{}^{}".format(self.coefficient, self.variable, self.power)
# Examples of algebraic terms of varying coefficients, variables, and powers.
expression_1 = ExpressionMaker(7, "x", 1)
expression_2 = ExpressionMaker(4, "y", 1)
expression_3 = ExpressionMaker(-3, "a", 2)
# Make the program understand what the user inputs and convert it into an algebraic expression.
# Make the program add the algebraic expressions from the user.
# An algebraic term has: a coefficient, a variable, and a power.
# Let the program check if the input has those 3 and if they are in the right order.
expression_holder1 = input("What is your first algebraic expression?: ")
print("You first algebraic expression is: " + expression_holder1)
I'm really not sure what to do after this. The most I've been able to think about was to use "If statements" to check if the expression_holders have an integer(for the coefficient), string(for the variable), and I don't know what to check for the power. I also don't know how to check if the order is correct. For example, the correct input would be 7x^3 but what if they instead type x7^3. And if the input is wrong, how do I let the user know that and let them type again?
This solution uses regular expression to split the coefficient and variable apart. The power is retrieved using parition. It prints an error if the format of the given input is incorrect.
import re #adds regular expression module
#Code for class can go here
while True:
try:
expression_holder1 = input("What is your first algebraic expression?: ")
expression = expression_holder1.partition("^") #Paritions the expression using the ^, the power if stored in the 2nd index
coefficient_variable = expression[0] #THe coefficient and varible is stored in the first index
res = re.split('(\d+)', coefficient_variable) #Uses regular expression to split the character and number
power = expression[2] # Stores the value of given power
coefficient = res[1] # store the value of coefficient
variable = res[2] # Stores the varible given
if power.isdigit() == False or coefficient.isdigit() == False or variable.isalpha() == False:
print("Wrong input") #prints error if the incorrect format is given
else:
break
except IndexError: #prints error if a coefficient, variable or power is missing
print("Index Error -> Wrong input")
expression1 = ExpressionMaker(coefficient, variable, power)
print(expression1.term())

Is isinstance() function not enough to detect integers and floats?

I'm making a very basic calculator as my first project (I'm learning Python and want something to show what i've learned so far) and as it is right now, the program works fine.
As it is right now, I have a function to validate user inputs so that the code only accepts numbers and decimal points, and returns the input as a float value to be calculated.
However, I would like to make it so that the code can recognize if the user is entering an Integer or a Float value, so that a simple operation such as "2+2" doesn't return a float "4.0" result, and just a "4".
The function that I have working right now is the following:
def valid_num():
number = ''
valid = True
while valid:
try:
number = float(input('───→ '))
break
except ValueError:
print('')
print('Invalid input (numbers and decimal points only).')
return number
To be able to diferentiate between integers and float values, I looked up methods and found the isinstance() function, which I may not be applying correctly... This is the function that I'm trying to make work:
def valid_num():
number = ''
valid = True
while valid:
number = input('───→ ')
check_int = isinstance(number, int)
if check_int:
number = int(number)
break
check_float = isinstance(number, float)
if check_float:
number = float(number)
break
if not check_int and not check_float:
print('Invalid input (numbers and decimal points only).')
return number
Sorry if there's an obvious error I'm not seeing, I'm just starting to code... Thanks in advance for any help you can provide =D
As pointed out by #tim-roberts, isinstance(number, int) checks the type of number, which is a string.
To distinguish ints and floats, you have a couple of options:
Try to convert it to int, if that fails try to convert to float; this would use a pair of try/except clauses.
Validate the input as text, checking that it follows the rules you set for your calculator, probably using regular expressions; this would let you positively assert that it matches what you expect, rather than whatever the int and float functions accept.
You could also work throughout in float (or Decimal), then convert 4.0 to 4 on output; that would mean that you'd also get "4" for "0.5 * 8" (which may be better or worse than getting "4.0"; that's up to your UX design).

Python - How to make one input correspond to several functions

My program should have a single input where you write either an arabic number, a roman number, or adding roman numbers. For example:
Year: 2001
... MMI
Year: LX
... 60
Year: MI + CI + I
... MCIII
Year: ABC
... That's not a correct roman numeral
Well, I guess you get the deal. First, I tried with something like this:
def main():
year = input ("Year: ")
if type(int(year)) == type(1):
arab_to_rome(year)
else:
rome_to_arab(year)
main()
This have obvious problems, firstly, everything else than an integer will be considered as roman numerals and it doesn't take addition in consideration.
Then I googled and found something called isinstance. This is the result of trying that:
def main(input):
year = input ("Year: ")
if isinstance(input,int):
arab_to_rome(year)
elif isinstance(input,str):
rome_to_arab (year)
else:
print ("Error")
main()
This has problems as well. I get an error message stating: Invalid syntax.
This code doesn't take addition in consideration either.
You can use a try/except block to see if the input is an int. This makes up the first 3 lines of the code. For more on that see this question.
year = input('Year: ')
try:
print(arab_to_rome(int(year)))
except ValueError:
result = 0
for numeral in year.split('+'):
numeral.replace(' ','')
result += rome_to_arab(numeral)
print(result)
The part after the except is a simple way to handle the addition, and you could migrate this into your rome_to_arab function if you wish. It splits the string on each + and then add the result of the Arabic calculations together to make a complete Arabic result. The replace() function gets rid of any extra spaces to make sure it doesn't break your other methods. See this question or more info on split() and replace().
As you haven't shown us the conversion methods I'm going to assume arab_to_rome returns a string and rome_to_arab returns an int. If not you should probably change them such that they do (not just for my example, but for good code convention)

Why does this Python 3.3 code not work? digc not defined

It prints diga and digb but doesnt work with c! Any help? It's supposed to be a Denary to Binary converter but only 1-64, once i've cracked the code will increase this! Thanks so much
denaryno=int(input("Write a number from 1-64 "))
if 64%denaryno > 0:
diga=0
remaindera=(64%denaryno)
if 32/denaryno<1:
digb=1
remainderb=(denaryno%32)
else:
digb =0
if 16/remainderb<1:
digc=1
remainderc=(denaryno%16)
else:
digc=0
if 8/remainderc<1:
digd=1
remainderd=(denaryno%8)
else:
digd=0
if 4/remainderd<1:
dige=1
remaindere=(denary%4)
else:
dige=0
if 2/remaindere<1:
digf=1
remainderf=(denary%2)
else:
digf=0
if 1/remainderf<1:
digg=1
remainderg=(denary%1)
else:
digg=0
print (str(diga)+str(digb))
You only set digc in one of the top if/else statement. If 32/denaryno<1 is True, you don't set digc at all.
Set digc at the top of the function (to 0 or whatever else you want it to be). This applies to all the digit variables, digd, dige, etc.
What you really should do, instead, is use a list of digits, and append either a 0 or a 1 to that list every time you divide the number by a factor.
You may want to take a look at the divmod() function; it returns both the quotient and the remainder. You could also do with some looping here to slash the number of if statements needed here:
number = int(input("Write a number from 1-64 "))
digits = []
factor = 64
while number:
quotient, number = divmod(number, factor)
digits.append(quotient)
factor //= 2
print(''.join(map(str, digits)))
Wow that was a lot of work, you don't have to do all that.
def bin_convert(x, count=8):
return "".join(map(lambda y:str((x>>y)&1), range(count-1, -1, -1)))
here are the functions comprising this one from easy->important
str() returns a string
range() is a way to get a list from 1 number to another. Written like this range(count-1, -1, -1) counts backwards.
"".join() is a way to take an iterable and put the pieces together.
map() is a way to take a function and apply it to an iterable.
lambda is a way to write a function in 1 line. I was being lazy and could have written another def func_name(y) and it would have worked just as well.
>> is a way to shift bits. (which I believe understanding this one is the key component to understanding your problem)

Parsing string to see if it can be cast to a float

Part of my homework assignment is to write a function that will parse a string such as '-.4e-4' and identify any problems that would prevent it from being cast to a float. For example, in '10e4.5' I would need to detect the decimal in the exponent and provide a relevant error message.
I have attempted many things. The first and, of course, most basic is the try: except:. Attempt to cast it to a float and let Python do the heavy lifting. However, as far as I can see, the errors it can return are not descriptive enough for this assignment.
The second thing I tried was to normalize the string, replacing all digits with n, signs with s, decimals with d, exponents with e (the maketrans function from C made this very fast). Then, I cut down any repeated n's to a single n. I made a list of all valid float formats and checked if the normalized string was in that list. AKA, I white-listed it. It worked perfectly and rather time-efficiently, but again, no error checking. That code is posted below.
import string,time
check_float_trans = string.maketrans("nsd0123456789-+.","???nnnnnnnnnnssd")
check_float_valids = 'n sn sndn ndn ndnen dn sdn sdnen sdnesn dnesn dnen nen nesn snesn sn snen sndnen sndnesn ndnesn'.split()
def check_float( test ):
"""Check if string <test> could be cast as a float, returns boolean."""
test = test.translate(check_float_trans)
test = ''.join([a for a,b in zip(test, ' '+test) if a != b])
return test in check_float_valids
I was hoping someone here could give me some pointers. I don't want this handed to me, but I am relatively stuck. I tried guardian-coding it, trying to identify reasons why the string might not be castable as a float, but I could never put up enough walls to ensure that no bad strings got a false positive.
Thanks.
Here's what I would do... (also this is untested)
def isValid(expression):
if 'e' in expression:
number, exponent = expression.split('e')
else:
print "not a valid format, no 'e' in expression"
return False
# a bunch of other if statments here to check for
#certain conditions like '.' in the exponent component
return float(number) ** float(exponent)
if __name__ == '__main__':
print isValid('-.4e-4')
print isValid('10e4.5')

Categories