Example #1
0
def main():

    while True:
        user_input = raw_input("> ")
        split_input = user_input.split()
        operator = split_input[0]

        if len(split_input) >= 2:
            first_operand = int(split_input[1])
        if len(split_input) == 3:  # change this if program takes more than 3 args
            second_operand = int(split_input[2])
        if operator == 'q':
            break
        else:
            if operator == "+":
                print arithmetic.add(first_operand, second_operand)
            elif operator == "-":
                print arithmetic.subtract(first_operand, second_operand)
            elif operator == "*":
                print arithmetic.multiply(first_operand, second_operand) 
            elif operator == "/":
                print arithmetic.divide(first_operand, second_operand)  
            elif operator == "square":
                print arithmetic.square(first_operand) 
            elif operator == "cube":
                print arithmetic.cube(first_operand)
            elif operator == "pow":
                print arithmetic.power(first_operand, second_operand)     
            elif operator == "mod":
                print arithmetic.mod(first_operand, second_operand)
            else:
                print "Operation not recognized."
Example #2
0
def get_user_input():
    user_input = raw_input("Please provide what you'd like to calculate: ")
    parse_input = user_input.split(" ")
    
    operator = parse_input[0]
    if operator == "q":
        quit()

    num1 = int(parse_input[1])

    if operator == "square" or operator == "cube":
        num2 = None
    else:
        num2 = int(parse_input[2])


    if operator == "+":
        print arithmetic.add(num1,num2)
    elif operator == "-":
        print arithmetic.subtract(num1,num2)
    elif operator == "*":
        print arithmetic.multiply(num1,num2)
    elif operator == "/":
        print arithmetic.divide(num1,num2)
    elif operator == "square":
        print arithmetic.square(num1)
    elif operator == "cube":
        print arithmetic.cube(num1)
    elif operator == "pow":
        print arithmetic.power(num1,num2)
    elif operator == "mod":
        print arithmetic.mod(num1,num2)
    else:
        print "Sorry, we can't do that."
Example #3
0
def calculator(input):

	token = input.split(" ")

	if token[0] == "+":
		print arithmetic.add(int(token[1]), int(token[2]))

	if token[0] == "-":
		print arithmetic.subtract(int(token[1]), int(token[2]))

	if token[0] == "*":
		print arithmetic.multiply(int(token[1]), int(token[2]))

	if token[0] == "/":
		print arithmetic.divide(int(token[1]), int(token[2]))

	if token[0] == "square":
		print arithmetic.square(int(token[1]), int(token[2]))

	if token[0] == "cube":
		print arithmetic.cube(int(token[1]), int(token[2]))

	if token[0] == "power":
		print arithmetic.power(int(token[1]), int(token[2]))

	if token[0] == "mod":
		print arithmetic.mod(int(token[1]), int(token[2]))
Example #4
0
def main():

    while True:
        
        #get operation and numbers from user
        user_input = raw_input("> ")
        #split operations and numbers
        tokens = user_input.split(" ")

        check = check_args(tokens) #This checks the number of arguments the user enters

        if check == "y":
            #find correct operation
            if tokens[0] == "+":
                print arithmetic.add(int(tokens[1]), int(tokens[2])) # check: can we do tokens[1, 2]?
                # make sure there are 2 arguments
            elif tokens[0] == "-":
                print arithmetic.subtract(int(tokens[1]), int(tokens[2])) # 2 args
            elif tokens[0] == "*":
                print arithmetic.multiply(int(tokens[1]), int(tokens[2])) # 2 args
            elif tokens[0] == "/":
                print arithmetic.divide(float(tokens[1]), int(tokens[2])) # zero
            elif tokens[0] == "square":
                print arithmetic.square(int(tokens[1])) # 1 arg
            elif tokens[0] == "cube":
                print arithmetic.cube(int(tokens[1])) # 1 arg
            elif tokens[0] == "pow":
                print arithmetic.power(int(tokens[1]), int(tokens[2])) # 2 args
            elif tokens[0] == "mod":
                print arithmetic.mod(int(tokens[1]), int(tokens[2])) # zero
Example #5
0
def do_math():
    while True:
        user_input = raw_input("> ").strip()
        if user_input == "q":
            break

        # separate user input 
        input_list = user_input.split()

        # determine math function to be called and check for errors
        if len(input_list) == 1 or len(input_list) == 2:
            if input_list[0] in ["+", "-", "*", "/", "pow", "mod"]:
                print "Oops! You need to give us more numbers! Try again."
            elif len(input_list) == 1 and input_list[0] in ["square", "cube"]:
                print "Oops!  You need to give us 1  number! Try again."
            elif len(input_list) == 2 and input_list[0] in ["square", "cube"]:
                # convert user input values to integers for math
                second = int(input_list[1])
                if  input_list[0] == "square":
                    print arithmetic.square(second)
                elif input_list[0] == "cube":
                    print arithmetic.cube(second)
            else:
                print "I don't understand what math operation you want me to do. Try again."

        elif len(input_list) >= 3:
            # convert user input values to integers for math
            second, third = int(input_list[1]), int(input_list[2])

            if input_list[0] == "+":
                print arithmetic.add(input_list[1:])
            elif input_list[0] == "-":
                print arithmetic.subtract(input_list[1:])
            elif input_list[0] == "*":
                print arithmetic.multiply(input_list[1:])
            elif input_list[0] == "/":
                print arithmetic.divide(input_list[1:])
            elif input_list[0] in ["square", "cube"]:
                print "Oops! You gave us too many numbers!  Just give 1. Try again."
            elif input_list[0] in ["pow", "mod"]:
                # error check for too many numbers
                if len(input_list) == 3:
                    if input_list[0] == "pow":
                        print arithmetic.power(second, third)
                    elif input_list[0] == "mod":
                        print arithmetic.mod(second, third)
                else:
                    print "Oops! You gave us too many numbers!  Just give 2. Try again."
            else:
                print "I don't understand what kind of math you want me to do."
        else:
            print "I don't understand.  You said too much.  Talk less."
def soup_nazi_calculator():
    user_input = raw_input("Please provide what you'd like to calculate: ")
    parse_input = user_input.split(" ")
    list_length = len(parse_input)
    operator = parse_input[0]

    
    if operator == "q":
        quit()

    if str.isdigit(parse_input[1]) == True:
        num1 = int(parse_input[1])
    else:
        print "Please use numbers. This is math."
        return 

    if list_length < 3:
            num2 = None
    elif list_length == 3:
        if str.isdigit(parse_input[2]) == True:
            num2 = int(parse_input[2])
        else:
            print "Please use numbers. This is math."
            return
    elif list_length > 3:
        print "Sorry we only take up to 2 numbers"
        return



    if operator == "+":
        print arithmetic.add(num1,num2)
    elif operator == "-":
        print arithmetic.subtract(num1,num2)
    elif operator == "*":
        print arithmetic.multiply(num1,num2)
    elif operator == "/":
        print arithmetic.divide(num1,num2)
    elif operator == "square":
        print arithmetic.square(num1)
    elif operator == "cube":
        print arithmetic.cube(num1)
    elif operator == "pow":
        print arithmetic.power(num1,num2)
    elif operator == "mod":
        print arithmetic.mod(num1,num2)
    else:
        print "Sorry, we can't do that. The operators I take are +, -, *, /, square, cube, pow, mod, or q"
def utilize_userinput():
    operation = raw_input("Please enter the calculation you want>>>")
# asks user to input the operation they want
    operation = operation.split(" ") 

    num1 = int(operation[1])
    if len(operation) > 2:
        num2 = int(operation[2]) # accepts integers
        
    if operation[0] == "add":
        return arithmetic.add(num1, num2)
    elif operation[0] == "subtract":
        return arithmetic.subtract(num1, num2)
    elif operation[0] == "divide":
        return arithmetic.divide(num1, num2)
    elif operation[0] == "multiply":
        return arithmetic.multiply(num1, num2)
    elif operation[0] == "power":
        return arithmetic.power(num1, num2)
    elif operation[0] == "square":
        return arithmetic.square(num1)
    elif operation[0] == "cube":
        return arithmetic.cube(num1)
    elif operation[0] == "mod":
        return arithmetic.mod(num1, num2)
    else:
        print "Please enter valid operation."
Example #8
0
def three_count_functions(token_list):
    # run three count functions       
    if token_list[0] == "+":
        print arithmetic.add(token_list[1], token_list[2])
    elif token_list[0] == "-":
        print arithmetic.subtract(token_list[1], token_list[2])
    elif token_list[0] == "*":
        print arithmetic.multiply(token_list[1], token_list[2])
    elif token_list[0] == "/":
        print arithmetic.divide(token_list[1], token_list[2])
    elif token_list[0] == "pow":
        print arithmetic.power(token_list[1], token_list[2])
    elif token_list[0] == "mod":
        print arithmetic.mod(token_list[1], token_list[2])
    else:
        print "Too many functions. Input again."
Example #9
0
def main():
    # Your code goes here
    user_input = raw_input('> ')

    calculator_input = user_input.split(" ")
    user_math_operator = calculator_input[0]

    if calculator_input[1].isdigit() == True:
        first_num = int(calculator_input[1])
    else:
        print "Please enter a number"

    if calculator_input[2].isdigit() == True:
        second_num = int(calculator_input[2])
    else:
        print "Please enter a number"


    if user_math_operator == "+":
        result = arithmetic.add(first_num, second_num)
        print result
    elif user_math_operator == "-":
        result = arithmetic.subtract(first_num, second_num)
        print result
    elif user_math_operator == "/":
        result = arithmetic.divide(first_num, second_num)
        print result
    elif user_math_operator == "mod":
        result = arithmetic.mod(first_num, second_num)
        print result
    else:
        return 0
Example #10
0
def evaluate_input(input):
  calculation = 0
  fn = input[0]
  numbers = input[1]

  #brute force
  if(len(numbers) > 1):
    num1 = int(numbers[0])
    num2 = int(numbers[1])
    if(fn == '+'):
      calculation = arithmetic.add(num1, num2)
    if(fn == '-'):
      calculation = arithmetic.subtract(num1, num2)
    if(fn == '*'):
      calculation = arithmetic.multiply(num1, num2)
    if(fn == '/'):
      calculation = arithmetic.divide(num1, num2)
    if(fn == '%'):
      calculation = arithmetic.mod(num1, num2)
  else:
    numbers = int(numbers.pop())
    if(fn == '^2'):
      calculation = arithmetic.square(numbers)
    if(fn == '^3'):
      calculation = arithmetic.cube(numbers)
    if(fn == '^'):
      calculation = arithmetic.power(numbers)
  
  return calculation
Example #11
0
def main():
    # Your code goes here

    while True:
        user_input = raw_input("enter your function and numbers RPN style ")
        token = user_input.split()
        command = token[0]
        if command == "q":
            break
        elif command == "+":
            print(arithmetic.add(float(token[1]), float(token[2])))
        elif command == "-":
            print(arithmetic.subtract(float(token[1]), float(token[2])))
        elif command == "*":
            print(arithmetic.multiply(float(token[1]), float(token[2])))
        elif command == "/":
            print(arithmetic.divide(float(token[1]), float(token[2])))
        elif command == "square":
            print(arithmetic.square(float(token[1])))
        elif command == "cube":
            print(arithmetic.cube(float(token[1])))
        elif command == "pow":
            print(arithmetic.power(float(token[1]), float(token[2])))
        elif command == "mod":
            print(arithmetic.mod(float(token[1]), float(token[2])))
        else:
            print "I don't understand."
def soup_nazi_calculator():
    user_input = raw_input("Please provide what you'd like to calculate: ")
    parse_input = user_input.split(" ")
    list_length = len(parse_input)
    operator = parse_input[0]

    
    if operator == "q":
        quit()

    while True:
        try:
            num1 = int(parse_input[1])
            if list_length < 3:
                num2 = None
            elif list_length == 3:
                num2 = int(parse_input[2])
            elif list_length > 3:
                print "Sorry we only take up to 2 numbers"
                print "No calculator for you."
                quit()

            break
        except ValueError:
            print "That's not a number."
            print "No calculator for you."
            quit()

    if operator == "+":
        print arithmetic.add(num1,num2)
    elif operator == "-":
        print arithmetic.subtract(num1,num2)
    elif operator == "*":
        print arithmetic.multiply(num1,num2)
    elif operator == "/":
        print arithmetic.divide(num1,num2)
    elif operator == "square":
        print arithmetic.square(num1)
    elif operator == "cube":
        print arithmetic.cube(num1)
    elif operator == "pow":
        print arithmetic.power(num1,num2)
    elif operator == "mod":
        print arithmetic.mod(num1,num2)
    else:
        print "Sorry, we can't do that. The operators I take are +, -, *, /, square, cube, pow, mod, or q"
Example #13
0
def main():
    print "Welcome to the calculator."

    user_input = raw_input("Please specify numbers to process, \nstarting with the operand, \
separated by a single space: ")

    user_input_list = user_input.split(' ')
    #print user_input_list[0]

    if user_input_list[0] == "":
        del(user_input_list[0]) 

    if user_input_list[0] == "q":
        print "I'm quitting."
        exit(0)

    product = 1

    if user_input_list[0] not in ["+", "-", "/", "*", "mod", "square", "pow", "cube"]:
        print "I don't understand. Please use the format specified above."

    elif user_input_list[0] == "+": 
        print sum(turn_to_int(user_input_list))
    elif user_input_list[0] == "-":
        print arithmetic.subtract(int(user_input_list[1]), int(user_input_list[2]))
    elif user_input_list[0] == "/":
        divide_list = turn_to_int(user_input_list)
        divide = divide_list[0]
        for i in divide_list[1: len(divide_list)]:
            divide = divide / i
        print divide 
        # print arithmetic.divide(int(user_input_list[1]), int(user_input_list[2]))
    elif user_input_list[0] == "*":
        product_list = turn_to_int(user_input_list)
        for i in product_list:
            product = product * i
        print product 
    elif user_input_list[0] == "mod":
        print arithmetic.mod(int(user_input_list[1]), int(user_input_list[2]))
    elif user_input_list[0] == "square":
        print arithmetic.square(int(user_input_list[1]))
    elif user_input_list[0] == "cube":
        print arithmetic.cube(int(user_input_list[1]))
    elif user_input_list[0] == "pow":
        print arithmetic.pow(int(user_input_list[1]), int(user_input_list[2]))
Example #14
0
def main():

    question_list = ["num"]

    while question_list[0] != "q":

        question = raw_input("> ")
# TO DO
# question != "q" enables ability to quit, but removes ability to do math
# removing question != "q" stops text from being entered but results in user can't quit
        if question.isdigit() == False and question != "q":
            print "That's dumb."
            continue

        question_list = question.split()
        question_list.extend([0, 0])
    
        first = int(question_list[1])
        second = int(question_list[2])

        if question_list[0] == "+":
            print arithmetic.add(first, second)

        if question_list[0] == "-":
            print arithmetic.subtract(first, second)

        if question_list[0] == "*":
            print arithmetic.multiply(first, second)

        if question_list[0] == "/":
            print arithmetic.divide(first, second)

        if question_list[0] == "**2" or question_list[0] == "square":
            print arithmetic.square(first)    

        if question_list[0] == "**3" or question_list[0] == "cube":
            print arithmetic.cube(first)

        if question_list[0] == "pow":
            print arithmetic.power(first, second)

        if question_list[0] == "%" or question_list[0] == "mod":
            print arithmetic.mod(first, second)
Example #15
0
def operation2(tokens):
    operator = tokens[0]
    num1 = int(tokens[1])
    num2 = int(tokens[2])

    if operator == "pow":
        result = arithmetic.power(num1, num2)
    elif operator == "mod":
        result = arithmetic.mod(num1, num2)
    else:
        result = arithmetic.divide(num1, num2)

    print result
Example #16
0
def main():    
    while True:
        number_input = raw_input("> ")
        tokens = number_input.split(" ")
        if tokens[0] == "q":
            quit()
        if tokens[0] == "+":
            print arithmetic.add(int(tokens[1]), int(tokens[2]))
        if tokens[0] == "-":
            print arithmetic.subtract(int(tokens[1]), int(tokens[2]))
        if tokens[0] == "*":
            print arithmetic.multiply(int(tokens[1]), int(tokens[2]))
        if tokens[0] == "/":
            print arithmetic.divide(int(tokens[1]), int(tokens[2]))
        if tokens[0] == "square":
            print arithmetic.square(int(tokens[1]))
        if tokens[0] == "cube":
            print arithmetic.cube(int(tokens[1]))
        if tokens[0] == "pow":
            print arithmetic.power(int(tokens[1]), int(tokens[2]))
        if tokens[0] == "mod":
            print arithmetic.mod(int(tokens[1]), int(tokens[2]))
Example #17
0
def main():
    # Your code goes here
    quit_calculator = False
    while quit_calculator == False:
        user_input = raw_input('> ')
        calculator_input = user_input.split(" ")
        user_math_operator = calculator_input[0]
        
        if calculator_input[1].isdigit() == True:
                first_num = int(calculator_input[1])
        else:
            print "Please enter a number"

        if calculator_input[2].isdigit() == True:
                second_num = int(calculator_input[2])
        else:
            print "Please enter a number"


        if user_math_operator == "+":
                result = arithmetic.add(first_num, second_num)
                print result
                continue
            elif user_math_operator == "-":
                result = arithmetic.subtract(first_num, second_num)
                print result
            elif user_math_operator == "/":
                result = arithmetic.divide(first_num, second_num)
                print result
            elif user_math_operator == "mod":
                result = arithmetic.mod(first_num, second_num)
                print result
            elif user_math_operator == "pow":
                result = arithmetic.power(first_num, second_num)
                print result
            elif user_math_operator == "*":
                result = arithmetic.multiply(first_num, second_num)
                print result
            elif user_math_operator == "square":
                result = arithmetic.square(first_num, second_num)
                print result
            elif user_math_operator == "cube":
                result = arithmetic.cube(first_num, second_num)
                print result

            elif user_math_operator == "q":
                quit_calculator = True

        else:
            return 0
Example #18
0
def main():
    #import arithmetic.py
    #loop while first user input is not q
    #get user input -did
    #split user input into tokens
    #check first token to find out operation
    #if else statements to determine next steps
    #execute operation and (return) and print

    while True:
        inp = raw_input("> ")
        tokens = inp.split(" ")
        if len(tokens) > 1:
            x = int(tokens[1])
        if len(tokens) > 2:
            y = int(tokens[2])

        if tokens[0] == "+":
            answer = arithmetic.add(x, y)
        elif tokens[0] == "-":
            answer = arithmetic.subtract(x, y)
        elif tokens[0] == "*":
            answer = arithmetic.multiply(x, y)
        elif tokens[0] == "/":
            answer = arithmetic.divide(x, y)
        elif tokens[0] == "square":
            answer = arithmetic.square(x)
        elif tokens[0] == "sqroot":
            answer = math.sqrt(x)
        elif tokens[0] == "cube":
            answer = arithmetic.cube(x)
        elif tokens[0] == "pow":
            answer = arithmetic.power(x, y)
        elif tokens[0] == "mod":
            answer = arithmetic.mod(x, y)
        elif tokens[0] == "q":
            break
        else:
            print "This is an invalid expression."
        
        if len(tokens) > 1:
            print answer
Example #19
0
def main():
    while True:
        input = raw_input()
        tokens = input.split(" ")
        #loop through list to identify digits using the range function
    
        for index in range(len(tokens)): #range helps us get at the indices of a list.
            if tokens[index].isdigit() == True: #checks if the string at specific index is a digit
                tokens[index] = int(tokens[index]) #turns item at index into an integer

        if tokens[0] == "q":
            break
        elif tokens[0] == "+":
            Output = arithmetic.add(tokens[1], tokens[2])
            print Output
        elif tokens[0] == "-":
            Output = arithmetic.subtract(tokens[1], tokens[2])
            print Output
        elif tokens[0] == "*":
            Output = arithmetic.multiply(tokens[1], tokens[2])
            print Output
        elif tokens[0] == "/":
            Output = arithmetic.divide(tokens[1], tokens[2])
            print Output
        elif tokens[0] == "square":
            Output = arithmetic.square(tokens[1])
            print Output
        elif tokens[0] == "cube":
            Output = arithmetic.cube(tokens[1])
            print Output
        elif tokens[0] == "mod":
            Output = arithmetic.mod(tokens[1], tokens[2])
            print Output
        else:
            print "Please use prefix style format."
            continue
Example #20
0
def arithmetic_stuff(user_input_from_func):

    operator = user_input_from_func[0]
    num1 = float(user_input_from_func[1])
    num2 = float(user_input_from_func[2])
    num3 = float(user_input_from_func[3])

    if operator == 'add':
        print(add(num1, num2))
    elif operator == 'subtract':
        print(subtract(num1, num2))
    elif operator == 'multiply':
        print(multiply(num1, num2))
    elif operator == 'divide':
        print(divide(num1, num2))
    elif operator == 'square':
        print(square(num1))
    elif operator == 'cube':
        print(cube(num1))
    elif operator == 'power':
        print(power(num1, num2))
    elif operator == 'mod':
        print(mod(num1, num2))
    '''our add_mult and add_cubes are not working, program returns error of 'add_mult' is not defined'''
Example #21
0
def operation(operator, nums):

	x = int(float(nums[0]))
	if len(nums) > 1:
		y = int(float(nums[1]))

	if operator == '+':
		return arithmetic.add(x, y)
	elif operator == '-':
		return arithmetic.subtract(x, y)
	elif operator == '/':
		return "{0:.6f}".format(arithmetic.divide(float(nums[0]), float(nums[1])))
	elif operator == '*':
		return arithmetic.multiply(x, y)
	elif operator == 'square':
		return arithmetic.square(x)
	elif operator == 'cube':
		return arithmetic.cube(x)
	elif operator == 'pow':
		return arithmetic.power(x, y)
	elif operator == 'mod':
		return arithmetic.mod(x, y)
	else:
		error()
Example #22
0
def calculator(input_string):
    """Reads a keyboard input string that is space delimited 
       and prints the result of the called function.
    """

    hell = 'warm'
    while not hell == 'frozen':
        string_as_list = input_string.split(' ')
        if string_as_list[0] == 'q':
            hell = "frozen"
        elif string_as_list[0] == 'pow':
            print(power(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '+':
            print(add(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '-':
            print(subtract(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '/':
            print(divide(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == 'square':
            print(square(float(string_as_list[1])))
            input_string = input('???')
        elif string_as_list[0] == 'cube':
            print(cube(float(string_as_list[1])))
            input_string = input('???')
        elif string_as_list[0] == 'mod':
            print(mod(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '*':
            print(multiply(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        else:
            input_string = input('Please provide relevant arguments.')
Example #23
0
# filename: test_math.py
# author: guy Whorley
# description: my tests for import

import arithmetic as math


print math.add(4,3)
print math.multiply(8,9)
print math.subtract(10,23)
print math.mod(10,3)
Example #24
0
    num2 = tokens[2]

    if operator == "+":
        result = add(num1, num2)

    elif operator == "-":
        result = subtract(num1, num2)

    elif operator == "*":
        result = multiply(num1, num2)

    elif operator == "/":
        result = divide(num1, num2)

    elif operator == "sq":
        result = square(num1)

    elif operator == "cube":
        result = cube(num1)

    elif operator == "pow":
        result = power(num1, num2)

    elif operator == "mod":
        result = mod(num1, num2)

    else:
        print("Invalid operator entered")
        continue

    print(result)
def main():

    instructions()

    while True:
        math_input = raw_input("> ")  # get math input from user

        tokens = math_input.split(" ")  # split math instructions at space

        if tokens[0] == "+":
            if len(tokens) != 3:
                print "Please use the format '+ 2 3'"
            else:
                print arithmetic.add(int(tokens[1]), int(tokens[2]))

        elif tokens[0] == "-":
            if len(tokens) != 3:
                print "Please use the format '- 2 3'"
            else:
                print arithmetic.subtract(int(tokens[1]), int(tokens[2]))

        elif tokens[0] == "*":
            if len(tokens) != 3:
                print "Please use the format '* 2 3'"
            else:
                print arithmetic.multiply(int(tokens[1]), int(tokens[2]))

        elif tokens[0] == "/":
            if len(tokens) == 3 and tokens[2] == "0":
                print "You cannot divide by zero! Try again."
            elif len(tokens) < 3:
                print "Please use the format '/ 2 3'"
            else:
                print arithmetic.divide(tokens[1], tokens[2])

        elif tokens[0] == "square":
            if len(tokens) > 2:
                print "Please use the format 'square 2'"
            else:
                print arithmetic.square(int(tokens[1]))

        elif tokens[0] == "cube":
            if len(tokens) > 2:
                print "Please use the format 'cube 3'"
            else:
                print arithmetic.cube(int(tokens[1]))

        elif tokens[0] == "pow":
            if len(tokens) != 3:
                print "Please use the format 'pow 2 3'"
            else:
                print arithmetic.power(int(tokens[1]), int(tokens[2]))

        elif tokens[0] == "mod":
            if len(tokens) == 3 and tokens[2] == "0":
                print "You cannot divide by zero! Try again."
            elif len(tokens) < 3:
                print "Please use the format 'mod 2 3'"
            else:
                print arithmetic.mod(int(tokens[1]), int(tokens[2]))

        elif len(tokens) > 3:
            print "Please try again with 2 numbers"

        elif tokens[0] == "instructions":
            instructions()

        elif tokens[0] == "q":
            return False

        else:
            print "Please try again. Confused? Try typing 'instructions.'"
Example #26
0
    if "q" in token:
        print("calculator shutting down...")
        break

    elif token[0] == "+":
        answer = add(token[1], token[2])

    elif token[0] == "-":
        answer = subtract(token[1], token[2])
    # pow 3 9
    elif token[0] == "*":
        answer = multiply(token[1], token[2])

    elif token[0] == "/":
        answer = divide(token[1], token[2])

    elif token[0] == "square":
        answer = square(token[1])

    elif token[0] == "cube":
        answer = cube(token[1])

    elif token[0] == "pow":
        answer = power(token[1], token[2])

    elif token[0] == "mod" or token[0] == "%":
        answer = mod(token[1], token[2])

    print(answer)
def my_calculator(lines=None):
    # set play to true
    play = True
    while play == True:
        # input expressions
        express = input("Please enter your math expression: ")
        # split expression into tokens
        input_token = express.split(" ")
        print(input_token)

        # cycle through number of tokens
        if len(input_token) == 1:
            if input_token[0].startswith("q"):
                play = False
            else:
                print("invalid expression")

        elif len(input_token) == 2:
            num1 = float(input_token[1])
            if input_token[0].lower().startswith("square"):
                print(square(num1))
            elif input_token[0].lower().startswith("cube"):
                print(cube(num1))
            else:
                print("Invalid expression")

        elif len(input_token) == 3:
            num1 = float(input_token[1])
            num2 = float(input_token[2])

            if input_token[0].startswith('+'):
                print(add(num1, num2))
            elif input_token[0].startswith('-'):
                print(subtract(num1, num2))
            elif input_token[0].startswith('*'):
                print(multiply(num1, num2))
            elif input_token[0].startswith('/'):
                print(divide(num1, num2))
            elif input_token[0].startswith('pow'):
                print(pow(num1, num2))
            elif input_token[0].startswith('mod'):
                print(mod(num1, num2))

            else:
                print("invalid expression. try again please")
        else:
            try:
                sequence_list = []
                for x in input_token[1:]:
                    sequence_list.append(float(x))
                if input_token[0].startswith('+'):
                    print(reduce(add, sequence_list))
                elif input_token[0].startswith('-'):
                    print(reduce(subtract, sequence_list))
                elif input_token[0].startswith('*'):
                    print(reduce(multiply, sequence_list))
                elif input_token[0].startswith('/'):
                    print(reduce(divide, sequence_list))
                elif input_token[0].startswith('pow'):
                    print(reduce(pow, sequence_list))
                elif input_token[0].startswith('mod'):
                    print(reduce(mod, sequence_list))
            except:
                print("invalid expression. try again please!")

    print("Thanks for calculating with us!")
Example #28
0
     break
 elif len(tokens) == 2:
     tokens[1] = int(tokens[1])
     if tokens[0] == "square":
         result = square(tokens[1])
     elif tokens[0] == "cube":
         result = cube(tokens[1])
 elif len(tokens) == 3:
     tokens[1] = int(tokens[1])
     tokens[2] = int(tokens[2])
     if tokens[0] == "+":
         result = add(tokens[1], tokens[2])
     elif tokens[0] == "-":
         result = subtract(tokens[1], tokens[2])
     elif tokens[0] == "*":
         result = multiply(tokens[1], tokens[2])
     elif tokens[0] == "/":
         result = divide(tokens[1], tokens[2])
     elif tokens[0] == "pow":
         result = power(tokens[1], tokens[2])
     elif tokens[0] == "mod":
         result = mod(tokens[1], tokens[2])
     elif tokens[0] == "cubes+":
         result = add_cubes(tokens[1], tokens[2])
 elif len(tokens) == 4:
     tokens[1] = int(tokens[1])
     tokens[2] = int(tokens[2])
     tokens[3] = int(tokens[3])
     if tokens[0] == "x+":
         result = add_mult(tokens[1], tokens[2], tokens[3])
 print(result)
Example #29
0
    if operator == '+':
        print(add(num1, num2))
    elif operator == '-':
        print(subtract(num1, num2))
    elif operator == '*':
        print(multiply(num1, num2))
    elif operator == '/':
        print(divide(num1, num2))
    elif operator == 'square':
        if len(tokens) > 2:
            print("Too many inputs.")
        else:
            print(square(num1))
    elif operator == 'power':
        print(power(num1, num2))
    elif operator == 'cube':
        if len(tokens) > 2:
            print("Too many inputs.")
        else:
            print(cube(num1))
    elif operator == 'mod':
        print(mod(num1, num2))
    else:
        print("""
        Allowed operators are: +, -, *, /, square, power, cube, mod. 

        Enter in the format: '* 4 5' 

        If you are done, enter q.
        """)
        elif nums[1] == 0:
            print "Cannot divide by zero!"
        else: 
            print art.divide(nums[0], nums[1])

    elif operator == 'square':
        if num_args != 1:
            print error_string %("square", "square", "one")
        else: 
            print art.square(nums[0])

    elif operator == 'cube':
        if num_args != 1:
            print error_string %("cube", "cube", "one")
        else: 
            print art.cube(nums[0])

    elif operator == 'pow':
        if num_args != 2:
            print error_string %("power", "pow", "two")
        else: 
            print art.power(nums[0], nums[1])

    elif operator == 'mod':
        if num_args != 2:
            print error_string %("mod", "mod", "two")
        elif nums[1] == 0:
            print "Cannot divide by zero!"
        else: 
            print art.mod(nums[0],nums[1])
Example #31
0
        subtraction = subtract(float(num1), float(num2))
        print(subtraction)
    elif calc == '*' or calc == 'multiply':
        #call multiplication function on token[1] and token[2]
        multiplication = multiply(float(num1), float(num2))
        print(multiplication)
    elif calc == '/' or calc == 'divide':
        #call division function on token[1] and token[2]
        division = divide(float(num1), float(num2))
        print(division)
    elif calc == 'square':
        #call square function on token[1] and token[2]
        squared = square(float(num1))
        print(squared)
    elif calc == 'cube':
        #call cube function on token[1] and token[2]
        cubed = cube(float(num1))
        print(cubed)
    elif calc == 'power':
        #call power function on token[1] and token[2]
        powered = power(float(num1), float(num2))
        print(powered)
    elif calc == 'mod':
        #call mod function on token[1] and token[2]
        mode = mod(float(num1), float(num2))
        print(mode)

#         else:
#             (decide which math function to call based on first token)
#             if the first token is 'pow':
#                   call the power function with the other two tokens
while True:
    user_input = input("> ")
    tokens = user_input.split(" ")
    numbers = []
    for each in tokens[1:]:
        each = float(each)
        numbers.append(each)

    if tokens[0] == "q":
        print("Thanks for using our calculator")
        break
    else:
        if tokens[0] == "+":
            print(add(numbers))
        elif tokens[0] == "-":
            print(subtract(numbers))
        elif tokens[0] == "*":
            print(multiply(numbers))
        elif tokens[0] == "/":
            print(divide(numbers))
        elif tokens[0] == "square":
            print(square(numbers))
        elif tokens[0] == "cube":
            print(cube(numbers))
        elif tokens[0] == "pow":
            print(power(numbers))
        elif tokens[0] == "mod":
            print(mod(numbers))

# Replace this with your code
Example #33
0
    divide,
    square,
    cube,
    power,
    mod,
)

while True:
    calculation = input("What would you like to calculate?")
    toke_calculation = calculation.split(' ')
    print(toke_calculation)
    if toke_calculation[0] == "q":
        break
    result = None
    if toke_calculation[0] == "+":
        result = add(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "-":
        result = subtract(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "*":
        result = multiply(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "/":
        result = divide(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "square":
        result = square(int(toke_calculation[1]))
    elif toke_calculation[0] == "cube":
        result = cube(int(toke_calculation[1]))
    elif toke_calculation[0] == "power":
        result = power(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "mod":
        result = mod(int(toke_calculation[1]), int(toke_calculation[2]))
    print(result)
Example #34
0
    # elif len(tokens) < 3:
    #     num2 = '0'
    # else:
    #     num2 = tokens[2]

    # if len(tokens) < 3:
    #     num2 = "0"
    # else:
    #     num2 = tokens[2]

    # if len(tokens) > 3:
    #     num3 = tokens[3]

    if operator == "+":
        result = add(float(tokens[1]), float(tokens[2]))
    elif operator == "-":
        result = subtract(float(tokens[1]), float(tokens[2]))
    elif operator == "*":
        result = multiply(float(tokens[1]), float(tokens[2]))
    elif operator == "/":
        result = divide(float(tokens[1]), float(tokens[2]))
    elif operator == "square":
        result = square(float(tokens[1]))
    elif operator == "cube":
        result = cube(float(tokens[1]))
    elif operator == "pow":
        result = power(float(tokens[1]), float(tokens[2]))
    elif operator == "mod":
        result = mod(float(tokens[1]), float(tokens[2]))

    print(result)
Example #35
0
def main():
    # Your code goes here
    quit_calculator = False
    while quit_calculator == False:
        user_input = raw_input('> ')
        calculator_input = user_input.split(" ")
        user_math_operator = calculator_input[0]

        if user_math_operator == 'q':
            quit_calculator = True
            return
        elif invalid_input(calculator_input):
            pass

        if calculator_input[1].isdigit() == True:
            first_num = int(calculator_input[1])
        else:
            print "Please enter a number"
            continue

        if calculator_input[2].isdigit() == True:
            second_num = int(calculator_input[2])
        else:
            print "Please enter a number"
            continue

        # if user_math_operator == 'q':
        #     quit_calculator = True
        #     return
        if user_math_operator == "+":
            result = arithmetic.add(first_num, second_num)
            print result
            # continue
        elif user_math_operator == "-":
            result = arithmetic.subtract(first_num, second_num)
            print result
        elif user_math_operator == "/":
            result = arithmetic.divide(first_num, second_num)
            print result
        elif user_math_operator == "mod":
            result = arithmetic.mod(first_num, second_num)
            print result
        elif user_math_operator == "pow":
            result = arithmetic.power(first_num, second_num)
            print result
        elif user_math_operator == "*":
            result = arithmetic.multiply(first_num, second_num)
            print result
        elif user_math_operator == "square":
            result = arithmetic.square(first_num, second_num)
            print result
        elif user_math_operator == "cube":
            result = arithmetic.cube(first_num, second_num)
            print result

        # elif user_math_operator == "q":
        #     quit_calculator = True
        #     return 0

        else:
            return 0
import arithmetic


while True:
    query = raw_input()
    token_list = query.split(" ")
    number_one=int(token_list[1])
    number_two=int(token_list[2])
    if token_list[0] == '+':
        print arithmetic.add (number_one, number_two)
    elif token_list[0] == '-':
        print arithmetic.subtract (number_one, number_two)
    elif token_list[0] == '*':
        print arithmetic.multiply (number_one, number_two)
    elif token_list[0] == '/':
        print arithmetic.divide (number_one, number_two)
    elif token_list[0] == 'square':
        print arithmetic.square (number_one)
    elif token_list[0] == 'cube':
        print arithmetic.cube (number_one)
    elif token_list[0] == 'pow':
        print arithmetic.power (number_one, number_two)
    elif token_list[0] == 'mod':
        print arithmetic.mod (number_one, number_two)
    else:
        print "I don't understand."
Example #37
0
def main():
    #arithmetic = open("arithmetic.py")
	# open the file from arithmetic.py
    #my_file = open("arithmetic.py")
    # get input from user
    #rray = []
    math_input = raw_input("> ")
    array = []
    array = math_input.split(" ")
    #print array

    character = str(array[0])
    num1 = int(array[1])
    if len(array) == 3:
        num2 = int(array[2])
#        print "Please enter the correct calculation format for prefix notation."
#    elif len(array) == 1:
#        print "Please enter at least one character and one number."
#    else:
#        print "Please only use one character and two numbers."

    while character == "+" or "-" or "*" or "/" or "square" or "cube" or "pow" or "mod" and len(array) == 2 or 3:
        try:
            if character == "+":
                print arithmetic.add(num1,num2)
                break
            elif character == "-":
                print arithmetic.subtract(num1,num2)
                break
            elif character == "*":
                print arithmetic.multiply(num1,num2)
                break
            elif character == "/":
                print arithmetic.divide(num1,num2)
                break
            elif character == "square":
                if len(array) > 2:
                    print "Square can only accept one number."
                    break
                else:
                    print arithmetic.square(num1)
                    break
            elif character == "cube":
                if len(array) > 2:
                    print "Cube can only accept one number."
                    break
                else:
                    print arithmetic.cube(num1)
                    break
            elif character == "pow":
                print arithmetic.power(num1,num2)
                break
            elif character == "mod":
                print arithmetic.mod(num1,num2)
                break
            else:
                print "Sorry, I do not understand. Please give me a mathematical character for calculation."
                break
        except UnboundLocalError:
            print "Sorry, please enter the correct format for prefix notation calulation."
            break
Example #38
0
def test_mod(x, N):
    xmodN = arithmetic.mod(x, N)
    if N == 0:
        assert xmodN == x
    else:
        assert xmodN == x % N
Example #39
0
def calculate_two_numbers():
    print("Welcome to the Calculator.")

    print('Please enter one of the following operators: add, substract, multiply, divide, square, cube, exponent, or remainder, followed by the numbers you would like to calculate. For example, "add 5 3". Press "q" to exit the calculator.')
    
    calculator_input = input("What would you like to calculate first? ")
    tokens = calculator_input.split(' ')
   
    operator = tokens[0]
    
    num1 = float(tokens[1])
    
    num2 = float(tokens[2])
   
    while True:
        if tokens == 'q':
            break

        elif operator == 'add':
            result = add(num1, num2)
            print(result)
            # break

        elif operator == 'substract':
            result = subtract(num1, num2)
            print(result)
            # break

        elif operator == 'multiply':
            result = multiply(num1, num2)
            print(result)
            # break

        elif operator == 'divide':
            result = divide(num1, num2)
            print(result)
            # break

        elif operator == 'square':
            result = square(num1)
            print(result)
            # break

        elif operator == 'cube':
            result = cube(num1)
            print(result)
            # break

        elif operator == 'exponent':
            result = power(num1, num2)
            print(result)
            # break

        elif operator == 'remainder':
            result = mod(num1, num2)
            print(result)
            # break
        

        elif operator == 'subtract':
            result = subtract(num1, num2)
            print(result)
Example #40
0

        if input_list[0] == '+':
            solution = arithmetic.add(num1, num2)
        elif input_list[0] == '-': 
            solution = arithmetic.subtract(num1, num2)
        elif input_list[0] == '*':
            solution = arithmetic.multiply(num1, num2)
        elif input_list[0] == '/':
            solution = arithmetic.divide(num1, num2)
        elif input_list[0] == 'square':
            solution = arithmetic.square(num1)
        elif input_list[0] == 'cube':
            solution = arithmetic.cube(num1)
        elif input_list[0] == 'pow':
            solution = arithmetic.power(num1, num2)
        elif input_list[0] == 'mod':
            solution = arithmetic.mod(num1, num2)

        else:
            print("Not a valid arithmetic operator. Please enter a real one.")
            continue

        print(solution)

        

    

# Your code goes here
Example #41
0
                    int(tokens[2])) is int:
                # Reinitialize validity to be true
                input_validity = True

                # Call pow function if first token matches 'pow'
                if tokens[0] == 'pow':
                    print(power(int(tokens[1]), int(tokens[2])))
                # Call add function if first token matches '+'
                if tokens[0] == '+':
                    print(add(int(tokens[1]), int(tokens[2])))
                # Call divide function if first token matches '/'
                if tokens[0] == '/':
                    print(divide(int(tokens[1]), int(tokens[2])))
                # Call subtract function if first token matches '-'
                if tokens[0] == '-':
                    print(subtract(int(tokens[1]), int(tokens[2])))
                # Call multiply function if first token matches '*'
                if tokens[0] == '*':
                    print(multiply(int(tokens[1]), int(tokens[2])))
                # Call square function if first token matches 'square'
                if tokens[0] == 'square':
                    print(square(int(tokens[1])))
                # Call cube function if first token matches 'cube'
                if tokens[0] == 'cube':
                    print(cube(int(tokens[1])))
                # Call mod function if first token matches 'mod'
                if tokens[0] == 'mod':
                    print(mod(int(tokens[1]), int(tokens[2])))
        except:
            print('You have to use prefix notation.')
Example #42
0
    # Read user input
    # user can quit, quit the program ('q')
    data = input("Give us Equation : ")
    if data == 'q':
        break
    # commands will be seperated by space, so we can split on spaces
    # Whole numbers only
    list_data=data.split(" ")
    list_data[1] = float(list_data[1])

    if len(list_data)>2:
        list_data[2] = float(list_data[2])
    print(list_data)
    if list_data[0] == '+':
        print(add(list_data[1], list_data[2]))
    elif list_data[0]== '-':
        print(subtract(list_data[1],list_data[2]))
    elif list_data[0]== '*':
        print(multiply(list_data[1],list_data[2]))
    elif list_data[0] == '/':
        print(divide(list_data[1], list_data[2]))
    elif list_data[0] == 'square':
        print(square(list_data[1]))
    elif list_data[0] == 'cube':
        print(cube(list_data[1]))
    elif list_data[0] == 'pow':
        print(power(list_data[1], list_data[2]))
    elif list_data[0]== 'mod':
        print(mod(list_data[1],list_data[2]))
    else:
        print("Invalid entry! Please try again!")
    operator = input_list[0]
    if operator == "q":
        break
# Match any conditions to the correct function
    if operator == "+":
        print arithmetic.add(int(input_list[1]), int(input_list[2]))

    elif operator == "-":
        print arithmetic.subtract(int(input_list[1]), int(input_list[2]))

    elif operator == "*":
        print arithmetic.multiply(int(input_list[1]), int(input_list[2]))

    elif operator == "/":
        print arithmetic.divide(float(input_list[1]), float(input_list[2]))

    elif operator == "square":
        print arithmetic.square(float(input_list[1]))

    elif operator == "cube":
        print arithmetic.cube(float(input_list[1]))

    elif operator == "pow":
        print arithmetic.power(float(input_list[1]))

    elif operator == "mod":
        print arithmetic.mod(float(input_list[1], input_list[2]))

    else: 
        print "that's not relevant input"
Example #44
0
            exp_tokens[index] = int(exp_tokens[index])

        if exp_tokens[0] == "+":
            answer = (add(exp_tokens[1], exp_tokens[2]))

        elif exp_tokens[0] == "-":
            answer = subtract(exp_tokens[1], exp_tokens[2])

        elif exp_tokens[0] == "*":
            answer = multiply(exp_tokens[1], exp_tokens[2])

        elif exp_tokens[0] == "/":
            answer = divide(exp_tokens[1], exp_tokens[2])

        elif exp_tokens[0] == "square":
            answer = square(exp_tokens[1])

        elif exp_tokens[0] == "cube":
            answer = cube(exp_tokens[1])

        elif exp_tokens[0] == "pow":
            answer = power(exp_tokens[1], exp_tokens[2])

        elif exp_tokens[0] == "mod":
            answer = mod(exp_tokens[1], exp_tokens[2])

        else:
            answer = 'Invalid expression.'

    print(answer)
Example #45
0
        tokens.append(float(num))

    try:
        if operator == '+':
            print(reduce(add, tokens))

        elif operator == '-':
            print(reduce(subtract, tokens))

        elif operator == '*':
            print(reduce(multiply, tokens))

        elif operator == '/':
            print(divide(float(tokens[1]), float(tokens[2])))

        elif operator == 'square':
            print(square(float(tokens[1])))

        elif operator == 'cube':
            print(cube(float(tokens[1])))

        elif operator == 'pow':
            print(power(float(tokens[1]), float(tokens[2])))

        elif operator == 'mod':
            print(mod(float(tokens[1]), float(tokens[2])))
    except:
        print("invalid input")

    user_input = input("")
Example #46
0
    if (token[0] == "q"):
        break
    else:
        if (token[0] == 'add'):
            result = add(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'subtract'):
            result = subtract(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'multiply'):
            result = multiply(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'divide'):
            result = divide(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'square'):
            result = square(int(token[1]))
            print(result)
        elif (token[0] == 'cube'):
            result = cube(int(token[1]))
            print(result)
        elif (token[0] == 'power'):
            result = power(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'mod'):
            result = mod(int(token[1]), int(token[2]))
            print(result)
        else:
            break
     if token[0] == "square":
         result = math.square(token[1])
     elif token[0] == "cube":
         result = math.cube(token[1])
     elif token[0] == "+":
         result = math.add(result, token[1])
     elif token[0] == "-":
         result = math.subtract(result, token[1])
     elif token[0] == "*":
         result = math.multiply(result, token[1])
     elif token[0] == "/":
         result = math.divide(result, token[1])
     elif token[0] == "pow":
         result = math.power(result, token[1])
     elif token[0] == "mod":
         result = math.mod(result, token[1])
     else:
         print "Please enter a valid expression."
 else:
     try:
         token[1] = int(token[1])
         token[2] = int(token[2])
     except ValueError:
         print "Please enter a valid expression."
         continue
     if token[0] == "+":
         result = math.add(token[1], token[2])
     elif token[0] == "-":
         result = math.subtract(token[1], token[2])
     elif token[0] == "*":
         result = math.multiply(token[1], token[2])
Example #48
0
  # read input
  problem = raw_input("enter your problem: ")
  # tokenize input
  tokens = problem.split(" ")
  if len(tokens) > 1:
    num1 = float(tokens[1])
  if len(tokens) > 2:
    num2 = float(tokens[2])

  # if the first token is 'q', quit
  if tokens[0] == "q":
    print "goodbye!"
    break
  # otherwise decide which math function to call based on the tokens we read
  if tokens[0] == '+':
    print arithmetic.add(num1, num2)
  elif tokens[0] == '-':
    print arithmetic.subtract(num1, num2)
  elif tokens[0] == '*':
    print arithmetic.multiply(num1, num2)
  elif tokens[0] == '/':
    print arithmetic.divide(num1, num2)
  elif tokens[0] == 'square':
    print arithmetic.square(num1)
  elif tokens[0] == 'cube':
    print arithmetic.cube(num1)
  elif tokens[0] == 'pow':
    print arithmetic.power(num1, num2)
  elif tokens[0] == 'mod':
    print arithmetic.mod(num1, num2)
Example #49
0
def calculator():

    while True:
        user_input = input("enter your equation > ")

        tokens_from_user_input = user_input.split(' ')

        if tokens_from_user_input[0] == "q":
            return False

        if tokens_from_user_input[0] == "+":

            num1 = int(tokens_from_user_input[1])
            num2 = int(tokens_from_user_input[2])

            print(add(num1, num2))

        if tokens_from_user_input[0] == "-":

            num1 = int(tokens_from_user_input[1])
            num2 = int(tokens_from_user_input[2])

            print(subtract(num1, num2))

        if tokens_from_user_input[0] == "*":

            num1 = int(tokens_from_user_input[1])
            num2 = int(tokens_from_user_input[2])

            print(multiply(num1, num2))

        if tokens_from_user_input[0] == "/":

            num1 = int(tokens_from_user_input[1])
            num2 = int(tokens_from_user_input[2])

            print(divide(num1, num2))

        if tokens_from_user_input[0] == "square":

            num1 = int(tokens_from_user_input[1])

            print(square(num1))

        if tokens_from_user_input[0] == "cube":

            num1 = int(tokens_from_user_input[1])

            print(cube(num1))

        if tokens_from_user_input[0] == "pow":

            num1 = int(tokens_from_user_input[1])
            num2 = int(tokens_from_user_input[2])

            print(power(num1, num2))

        if tokens_from_user_input[0] == "mod":

            num1 = int(tokens_from_user_input[1])
            num2 = int(tokens_from_user_input[2])

            print(mod(num1, num2))
Example #50
0
        tokens2 = float(tokens[2])
        print(arithmetic.subtract(tokens1, tokens2))

    if tokens[0] == '*':
        tokens1 = float(tokens[1])
        tokens2 = float(tokens[2])
        print(arithmetic.multiply(tokens1, tokens2))

    if tokens[0] == '/':
        tokens1 = float(tokens[1])
        tokens2 = float(tokens[2])
        print(arithmetic.divide(tokens1, tokens2))

    if tokens[0] == 'square':
        tokens1 = float(tokens[1])
        print(arithmetic.square(tokens1))

    if tokens[0] == 'cube':
        tokens1 = float(tokens[1])
        print(arithmetic.cube(tokens1))    

    if tokens[0] == 'pow':
        tokens1 = float(tokens[1])
        tokens2 = float(tokens[2])
        print(arithmetic.power(tokens1, tokens2))

    if tokens[0] == 'mod':
        tokens1 = float(tokens[1])
        tokens2 = float(tokens[2])
        print(arithmetic.mod(tokens1, tokens2))   
Example #51
0
while calculator == True:
    input_string = input('>')
    token = input_string.split(' ')
    if token[0] == 'q':
        calculator = False
    else:
        a = int(token[1])
        if token[0] == '+':
            b = int(token[2])
            x = add(a, b)
        elif token[0] == '-':
            b = int(token[2])
            x = subtract(a, b)
        elif token[0] == '*':
            b = int(token[2])
            x = multiply(a, b)
        elif token[0] == '/':
            b = int(token[2])
            x = divide(a, b)
        elif token[0] == 'square':
            x = square(a)
        elif token[0] == 'cube':
            x = cube(a)
        elif token[0] == 'pow':
            b = int(token[2])
            x = power(a, b)
        elif token[0] == 'mod':
            b = int(token[2])
            x = mod(a, b)
        print(x)
        # tokens[2] = int(tokens[2])
        tokens = makeInt(tokens)
        print arithmetic.subtract(tokens[1], tokens[2])

    elif tokens[0] == "*":
        tokens = makeInt(tokens)
        print arithmetic.multiply(tokens[1], tokens[2])

    elif tokens[0] == "/":
        tokens = makeInt(tokens)
        print arithmetic.divide(tokens[1], tokens[2])

    elif tokens[0] == "square":
        tokens = makeInt(tokens)
        print arithmetic.square(tokens[1])

    elif tokens[0] == "cube":
        tokens = makeInt(tokens)
        print arithmetic.cube(tokens[1])

    elif tokens[0] == "pow":
        tokens = makeInt(tokens)
        print arithmetic.power(tokens[1], tokens[2])

    elif tokens[0] == "%":
        tokens = makeInt(tokens)
        print arithmetic.mod(tokens[1], tokens[2])

    else:
        print "I don't know what that means."
def main():
    # begin program
    print "This math calculator uses prefix notation. For example in prefix notation,"
    print "the same statement would be written '+ 3 2' or '* 4 4'."
    print "You can use +, -, *, /, square, cube, pow, or mod for calculation."
    print "To exit the calculator, press CTRL + D."
    
    # using while loop for calculation
    while True:
        # using try & except to catch errors
        try:
            # get input from user
            math_input = raw_input("> ")
            # creates an empty array
            array = []
            # separating the values given from user and inserting them to the array
            array = math_input.split(" ")
            # assigning array[0] to character
            character = str(array[0])
            # assigning array[1] to num1
            num1 = int(array[1])
            # checking for a third value
            if len(array) == 3:
                # assigning array[2] to num2
                num2 = int(array[2])
            # using try & except to catch any errors from user's inputs
            if character == "+" or "-" or "*" or "/" or "square" or "cube" or "pow" or "mod" and len(array) == 2 or 3:
                try:
                    # if statements for all kinds of calculation
                    if character == "+":
                        print arithmetic.add(num1,num2)
                        #break
                    elif character == "-":
                        print arithmetic.subtract(num1,num2)
                        #break
                    elif character == "*":
                        print arithmetic.multiply(num1,num2)
                        #break
                    elif character == "/":
                        print arithmetic.divide(num1,num2)
                        #break
                    elif character == "square":
                        # check for only one value needed for square
                        if len(array) > 2:
                            print "Sorry, square can only accept one number."
                            #break
                        else:
                            print arithmetic.square(num1)
                            #break
                    elif character == "cube":
                        # check for only one value needed for cube
                        if len(array) > 2:
                            print "Sorry, cube can only accept one number."
                            #break
                        else:
                            print arithmetic.cube(num1)
                            #break
                    elif character == "pow":
                        print arithmetic.power(num1,num2)
                        #break
                    elif character == "mod":
                        print arithmetic.mod(num1,num2)
                        #break
                    else:
                        print "Sorry, I do not understand. Please give me a mathematical character for calculation."
                        #break
                except UnboundLocalError:
                    print "Sorry, please enter the correct format for prefix notation calulation."
                    print "Or enter only two numbers after the prefix."
                    #break
        except IndexError:
            print "Sorry, please enter only up to one character and two numbers."
Example #54
0
    tokens1 = float(tokens[1])
    tokens2 = float(tokens[2])

    if tokens[0] == "q":
        break
    else:
        if tokens[0] == "add":
            a = add(tokens1, tokens2)
            print(a)
        if tokens[0] == "subtract":
            b = subtract(tokens1, tokens2)
            print(b)
        if tokens[0] == "multiply":
            c = multiply(tokens1, tokens2)
            print(c)
        if tokens[0] == "divide":
            d = divide(tokens1, tokens2)
            print(d)
        if tokens[0] == "square":
            e = square(tokens1, tokens2)
            print(e)
        if tokens[0] == "cube":
            f = cube(tokens1, tokens2)
            print(f)
        if tokens[0] == "power":
            g = power(tokens1, tokens2)
            print(g)
        if tokens[0] == "mod":
            h = mod(tokens1, tokens2)
            print(h)
Example #55
0
    if operator == "+":
        result = add(num1, num2)

    elif operator == "-":
        result = subtract(num1, num2)

    elif operator == '*':
        result = multiply((num1),(num2))

    elif operator == '/':
        result = divide((num1), (num2))

    elif operator == 'square':
        result = square((num1))

    elif operator == 'cube':
        result = cube((num1))

    elif operator == 'pow':
        result = power((num1), (num2))

    elif operator == 'mod':
        result = mod((num1),(num2))

    print(result)



# Replace this with your code
Example #56
0
        num2 = expression_tokens[2]

    #list of operations here:

    if operator == "+":
        answer = add(int(num1), int(num2))

    elif operator == "-":
        answer = subtract(int(num1), int(num2))

    elif operator == "*":
        answer = multiply(int(num1), int(num2))

    elif operator == "/":
        answer = divide(int(num1), int(num2))

    elif operator == "square":
        answer = square(int(num1))

    elif operator == "cube":
        answer = cube(int(num1))

    elif operator == "pow":
        answer = power(int(num1), int(num2))

    elif operator == "mod":
        answer = mod(int(num1), int(num2))

    #print the answer
    print(answer)
Example #57
0
	my_input = raw_input("> ")
	token = my_input.split(" ")
	if token[0] == 'q':
		break
	elif token[0] == 'add':
		result = add(token[1], token[2])
		print result
	elif token[0] == 'subtract':
		result = subtract(token[1], token[2])
		print result
	elif token[0] == 'multiply':
		result = multiply(token[1], token[2])
		print result
	elif token[0] == 'divide':
		result = divide(token[1], token[2])
		print result
	elif token[0] == 'square':
		result = square(token[1])
		print result
	elif token[0] == 'cube':
		result = cube(token[1])
		print result
	elif token[0] == 'power':
		result = power(token[1], token[2])
		print result
	elif token[0] == 'mod':
		result = mod(token[1], token[2])
		print result
	else:
		print "I don't know that one"
Example #58
0
    num2 = tokens[2]

    result = None

    if operator == '+':
        result = add(float(num1), float(num2))

    elif operator == '-':
        result = subtract(float(num1), float(num2))

    elif operator == '*':
        result = multiply(float(num1), float(num2))

    elif operator == '/':
        result = divide(float(num1), float(num2))

    elif operator == 'square':
        result = square(float(num1))

    elif operator == 'cube':
        result = cube(float(num1))

    elif operator == 'pow':
        result = power(float(num1), float(num2))

    elif operator == 'mod':
        result = mod(float(num1), float(num2))

    print(result)
Example #59
0
            continue

        if (tokens[0] == "+"):
            print(add(num))

        elif (tokens[0] == "-"):
            print(subtract(num))

        elif (tokens[0] == "*"):
            print(multiply(num))

        elif (tokens[0] == "/"):
            print(divide(num))

        elif (tokens[0] == "square"):
            print(square(num))

        elif (tokens[0] == "cube"):
            print(cube(num))

        elif (tokens[0] == "pow"):
            print(power(num))

        elif (tokens[0] == "mod"):
            print(mod(num))

        elif (tokens[0] == "add_mult"):
            print(add_mult(num))

        elif (tokens[0] == "add_cubes"):
            print(add_cubes(num))