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 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 #4
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 #5
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 #6
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 #7
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."
Example #8
0
def main():
    while True:
        input = raw_input()
        split_string = input.split()

        if split_string[0] == 'q':
            break

        else:
            if split_string[0] == '+':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.add(num1, num2)

            elif split_string[0] == '-':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.subtract(num1, num2)

            elif split_string[0] == '*':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.multiply(num1, num2)

            elif split_string[0] == '/':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.divide(num1, num2)

            elif split_string[0] == 'square':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.square(num1, num2)

            elif split_string[0] == 'cube':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.cube(num1, num2)

            elif split_string[0] == 'pow':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.power(num1, num2)

            elif split_string[0] == 'mod':
                num1 = int(split_string[1])
                num2 = int(split_string[2])
                print arithmetic.mod(num1, num2)

            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()

    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"
Example #10
0
def calculator():
    # Replace this with your code
    # #repeat forever:
    while True:
        #     read input (e.g. + 1 2)
        nums_input = input("Enter your equation: ")
        # tokenize input
        tokens = nums_input.split(' ')
        #         if the first token is "q":
        if tokens[0] == "q":
            #quit
            break
        elif tokens[0] == "+":
            print(add(int(tokens[1]), int(tokens[2])))
        elif tokens[0] == "-":
            print(subtract(int(tokens[1]), int(tokens[2])))
        elif tokens[0] == "*":
            print(multiply(int(tokens[1]), int(tokens[2])))
        elif tokens[0] == "/":
            print(divide(int(tokens[1]), int(tokens[2])))
        elif tokens[0] == "square":
            print(square(int(tokens[1])))
        elif tokens[0] == "cube":
            print(cube(int(tokens[1])))
        elif tokens[0] == "pow":
            print(power(int(tokens[1]), int(tokens[2])))
        elif tokens[0] == "mod":
            print(mod(int(tokens[1]), int(tokens[2])))
Example #11
0
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 #12
0
def main_calculator():
    while True:
        input_ = input()
        tokenized_input = input_.split(' ')
        if tokenized_input[0] == 'q':
            break
        else:
            if tokenized_input[0] == '+':
                print(add(int(tokenized_input[1]), int(tokenized_input[2])))
            if tokenized_input[0] == '-':
                print(
                    subtract(int(tokenized_input[1]), int(tokenized_input[2])))
            if tokenized_input[0] == '*':
                print(
                    multiply(int(tokenized_input[2]), int(tokenized_input[1])))
            if tokenized_input[0] == '/':
                print(divide(int(tokenized_input[1]), int(tokenized_input[2])))
            if tokenized_input[0] == 'square':
                print(square(int(tokenized_input[1])))
            if tokenized_input[0] == 'cube':
                print(cube(int(tokenized_input[1])))
            if tokenized_input[0] == 'pow':
                print(power(int(tokenized_input[1]), int(tokenized_input[2])))
            if tokenized_input[0] == 'mod':
                print(mod(int(tokenized_input[1]), int(tokenized_input[2])))
Example #13
0
def calculator():
    operator = tokens[0]
    num1 = float(tokens[1])
    num2 = float(tokens[2])

    try:
        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 == '**':
            result = square(num1)

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

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

    except IndexError:

        if operator == '**':
            result = square(num1)

        elif operator == 'cube':
            result = cube(num1)
    return result
Example #14
0
def calculator():
    """ Calculator for basic arithmetic functions"""

    while True:
        token_list = evaluate_input()
        token = token_list[0]
        #for token in token_list:
        if token ==  "q":
            break
        elif token == "+":
            print(ar.add(float(token_list[1]), float(token_list[2])))

        elif token == "-":
            print(ar.subtract(float(token_list[1]), float(token_list[2])))

        elif token == "*":
            print(ar.multiply(float(token_list[1]), float(token_list[2])))

        elif token == "/":
            print(ar.divide(float(token_list[1]), float(token_list[2])))

        elif token == "square":
            print(ar.square(float(token_list[1])))

        elif token == "cube":
            print(ar.cube(float(token_list[1])))

        elif token == "pow":
            print(ar.power(float(token_list[1]), float(token_list[2])))

        elif token == "mod":
            print(ar.mod(float(token_list[1]), float(token_list[2])))

        else:
            print("This isn't arithmetic!")
Example #15
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 #16
0
def tokenization(input_string):
    """Tokenize inputs for arithmetic operations"""

    tokens = input_string.split(' ')

    if tokens[0] == 'q' or tokens[0] == 'quit':
        return 'END'

    if 1 < len(tokens):  # is index 1 exist in list
        if tokens[0] == 'square':
            print(square(int(tokens[1])))

        elif tokens[0] == 'cube':
            print(cube(int(tokens[1])))

    if 2 < len(tokens):  # is index 2 exist in list
        if tokens[0] == '+':
            print(add(int(tokens[1]), int(tokens[2])))

        elif tokens[0] == '-':
            print(subtract(int(tokens[1]), int(tokens[2])))

        elif tokens[0] == '*':
            print(multiply(int(tokens[1]), int(tokens[2])))

        elif tokens[0] == '/':
            print(divide(int(tokens[1]), int(tokens[2])))

        elif tokens[0] == 'pow':
            print(power(int(tokens[1]), int(tokens[2])))

        elif tokens[0] == 'mod':
            print(mod(int(tokens[1]), int(tokens[2])))
Example #17
0
def calculator():
    operator = ''

    while operator != "q":
        user_input = input().split(" ")
        operator = user_input[0]

        if len(user_input) > 1:
            first_value = int(user_input[1])

        if len(user_input) >= 3: 
            second_value = int(user_input[2]) 

        if operator == "square":
            print (square(first_value))
        elif operator == "cube":
            print (cube(first_value))
        elif operator == "+":
            print (add(first_value, second_value))
        elif operator == "-":
            print (subtract(first_value, second_value))
        elif operator == "*":
            print (multiply(first_value, second_value))
        elif operator == "/":
            print (divide(first_value, second_value))
        elif operator == "pow":
            print (power(first_value, second_value))
        elif operator == "mod":
            print (mod(first_value, second_value))
Example #18
0
def calculator(input_string):
    """creates a calculator to reference functions in arithmetic.py"""
    #statement = input("Enter your arithmetic argument: ")
    statement = input_string.split(" ")
    first_token = statement[0]
    second_token = int(statement[1])
    third_token = int(statement[2])
    if first_token == "q":
        quit
    else:
        if first_token == "+":
            output = add(second_token, third_token)
        elif first_token == "-":
            output = subtract(second_token, third_token)
        elif first_token == "/":
            output = divide(second_token, third_token)
        elif first_token == "square":
            output = square(second_token, third_token)
        elif first_token == "cube":
            output = cube(second_token, third_token)
        elif first_token == "pow":
            output = power(second_token, third_token)
        elif first_token == "mod":
            output = mod(second_token, third_token)
        print(output)
        return output
Example #19
0
def do_arithmetic(operator, num1, num2=None):
    """Call function to calculate equation"""

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

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

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

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

    elif operator == "square":
        return square(num1)

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

    elif operator == "power":
        return power(num1, num2)

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

    else:
        print("Error")
Example #20
0
def calculator():
    while True:
        input_string = input('Enter string:')
        token = input_string.split(' ')
        oper = token[0]
        try:
            num1 = token[1]
        except:
            num1 = token[0]
        try:
            num2 = token[2]
        except:
            num2 = token[0]
    #token is a list
        if oper == 'q':
            quit()
        elif oper == "+":
            print(add(int(num1),int(num2)))
        elif oper == "-":
            print(subtract(int(num1),int(num2)))
        elif oper == "*":
            print(multiply(int(num1),int(num2)))
        elif oper == "/":
            print(divide(int(num1), int(num2)))
        elif oper == "square":
            print(square(int(num1)))
        elif oper == "cube":
            print(cube(int(num1)))
        elif oper == "pow":
            print(power(int(num1),int(num2)))
        elif oper == "mod":
            print(mod(int(num1),int(num2)))
Example #21
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 #22
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."
Example #23
0
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"
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"
Example #25
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 #26
0
def doMath(op, op1, op2):
    if op == "*":
        return arithmetic.multiply(op1,op2)
    elif op == "/":
        return arithmetic.divide(op1,op2)
    elif op == "+":
        return arithmetic.add(op1,op2)
    elif op == "^":
        return arithmetic.power(op1,op2)
    else:
        return arithmetic.subtract(op1,op2)
Example #27
0
def evaluateEqaution(operands,operators):

    if operators[0] == "+":
        return arithmetic.add(operands[0],operands[1])
    elif operators[0] == "-":
        return arithmetic.subtract(operands[0],operands[1])
    elif operators[0] == "*":
        return arithmetic.multiply(operands[0],operands[1])
    elif operators[0] == "/":
        return arithmetic.divide(operands[0],operands[1])
    elif operators == "^":
        return  arithmetic.power(operands[0],operands[1])
Example #28
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 #29
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 #30
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
def main():
    while True:  # repeat forever
        user_input = raw_input(">")
        parts = user_input.split()
        operator = parts[0]

        if operator == "q":
            return
            # Stop looping, exit the function
        elif operator == "+":
            num1 = int(parts[1])
            num2 = int(parts[2])
            print arithmetic.add(num1, num2)
        elif operator == "-":
            num1 = int(parts[1])
            num2 = int(parts[2])
            print arithmetic.subtract(num1, num2)
        elif operator == "*":
            num1 = int(parts[1])
            num2 = int(parts[2])
            print arithmetic.multiply(num1, num2)
        elif operator == "/":
            num1 = int(parts[1])
            num2 = int(parts[2])
        elif operator == "pow":
            num1 = int(parts[1])
            num2 = int(parts[2])
            print arithmetic.power(num1, num2)
        elif operator == "square":
            num1 = int(parts[1])
            print arithmetic.square(num1)
        elif operator == "cube":
            num1 = int(parts[1])
            print arithmetic.cub(num1)
        else:
            print "I don't know why you're trying to do."
def calculator():
    problem = ""
    while problem != "q":
        problem = input("Enter your equation > ")
        problemsplit = problem.split(' ')
        operator = problemsplit[0]

        if operator == "add" or operator == "+":
            num1 = float(problemsplit[1])
            num2 = float(problemsplit[2])
            print(add(num1, num2))

        elif operator == "subtract" or operator == "-":
            num1 = float(problemsplit[1])
            num2 = float(problemsplit[2])
            print(subtract(num1, num2))

        elif operator == "power" or operator == "**":
            num1 = float(problemsplit[1])
            num2 = float(problemsplit[2])
            print(power(num1, num2))

        elif operator == "multiply" or operator == "*":
            num1 = float(problemsplit[1])
            num2 = float(problemsplit[2])
            print(multiply(num1, num2))

        elif operator == "%" or operator == "mod":
            num1 = float(problemsplit[1])
            num2 = float(problemsplit[2])
            print(mod(num1, num2))

        elif operator == "divide" or operator == "/":
            num1 = float(problemsplit[1])
            num2 = float(problemsplit[2])
            print(divide(num1, num2))

        elif operator == "square":
            num1 = float(problemsplit[1])
            print(square(num1))

        elif operator == "cube":
            num1 = float(problemsplit[1])
            print(cube(num1))
Example #33
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 #34
0
def calculator():
    while True:
        userinput = input('Enter numbers:')
        token = userinput.split(' ')
        oper = token[0]
        if len(token) == 2:
            num1 = int(token[1])
        if len(token) == 3:
            num1 = int(token[1])
            num2 = int(token[2])
        if len(token) > 3:
            print('incorrect input')
        if token[0] == 'q':
            break
        elif token[0] == "+":
            add(num1, num2)
        elif token[0] == "-":
            subtract(num1, num2)
        elif oper == "square":
            print(square(num1))
        elif oper == "*":
            print(multiply(num1, num2))
        elif oper == "/":
            print(divide(num1, num2))
        elif oper == "cube":
            print(cube(num1))
        elif oper == "pow":
            print(power(num1, num2))
        elif oper == "mod":
            print(mod(num1, num2))

#def further():
    while True:
        userinput = input('Enter numbers:')
        token = userinput.split(' ')
        oper = token[0]
        num1 = float(token[1])
        if len(token) == 3:
            num2 = float(token[2])
        if len(token) >= 3:
            print('invalid entry')
Example #35
0
def calculator():
    while True:
        try:
            read_input = input("Enter your equation: ")
            tokens = read_input.split(" ")

            if "q" in tokens:
                print("Quit")
                break

            else:
                if tokens[0] == "+":  
                    answer = add(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "-":
                    answer = subtract(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "*":
                    answer = multiply(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "/":
                    answer = divide(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "square":
                    answer = square(float(tokens[1]))
                    print(answer)
                elif tokens[0] == "cube":
                    answer = cube(float(tokens[1]))
                    print(answer)
                elif tokens[0] == "pow":
                    answer = power(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "mod":
                    answer = mod(float(tokens[1]), float(tokens[2]))
                    print(answer)
                else:
                    print("Please type in an operator and 2 numbers separated by spaces.")
        except ValueError:
            print('That is not a number.')
Example #36
0
def calculator():
    """Interface for a calculator

    receives a string, breaks it into usable parts, outputs a number"""
    quit = False
    
    while quit is False: 
        math_equation = input("Enter your equation: ").split(" ")

        action = str(math_equation[0])
        if len(math_equation) >= 2:
            num1 = float(math_equation[1])

        if len(math_equation) == 3:
            num2 = float(math_equation[2])    

        # if action == "q" or action == "quit":
        #     quit = True

        if action == '+':
            print(add(num1, num2))
        elif action == '-':
            print(subtract(num1, num2))
        elif action == '*':
            print(multiply(num1, num2))
        elif action == '/':
            print(divide(num1, num2))
        elif action == 'square':
            print(square(num1))
        elif action == 'cube':
            print(cube(num1))
        elif action == 'pow':
            print(power(num1, num2))
        elif action == 'mod':
            print(mod(num1, num2))
        else:
            quit = True

    return "Bye!"
Example #37
0
def translate(tokens):
    if tokens[0] == "+":
        return add(float(tokens[1]), float(tokens[2]))
    elif tokens[0] == "-":
        return subtract(float(tokens[1]), float(tokens[2]))
    elif tokens[0] == "*":
        return multiply(float(tokens[1]), float(tokens[2]))
    elif tokens[0] == "/":
        return divide(float(tokens[1]), float(tokens[2]))
    elif tokens[0] == "square":
        return square(float(tokens[1]))
    elif tokens[0] == "cube":
        return cube(float(tokens[1]))
    elif tokens[0] == "pow":
        return power(float(tokens[1]), float(tokens[2]))
    elif tokens[0] == "mod":
        return mod(float(tokens[1]), float(tokens[2]))
    elif tokens[0] == "x+":
        return add_mult(float(tokens[1]), float(tokens[2]), float(tokens[3]))
    elif tokens[0] == "cubes+":
        return add_cubes(float(tokens[1]), float(tokens[2]))
    else:
        return "Please enter a valid operator."
def math_operations(input_list):

    input_length = len(input_list)

    operator = input_list[0]

    if input_length == 2:

        num1 = float(input_list[1])

    elif input_length == 3:
        num1 = float(input_list[1])
        num2 = float(input_list[2])

    if operator == "+":
        return arithmetic.add(num1, num2)

    elif operator == "-":
        return arithmetic.subtract(num1, num2)

    elif operator == "*":
        return arithmetic.multiply(num1, num2)

    elif operator == "/":
        return arithmetic.divide(num1, num2)

    elif operator == "square":
        return arithmetic.square(num1)

    elif operator == "cube":
        return arithmetic.cube(num1)

    elif operator == "pow":
        return arithmetic.power(num1, num2)

    elif operator == "mod":
        return arithmetic.mod(num1, num2)
Example #39
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 #40
0
def prefix_notation_calculator():
    """CLI application for a prefix-notation calculator."""
    try:

        operation_input[1] = float(operation_input[1])
        operation_input[2] = float(operation_input[2])
        if operation_input[0] == "+":
            return add(operation_input[1], operation_input[2])
        elif operation_input[0] == "-":
            return subtract(operation_input[1], operation_input[2])
        elif operation_input[0] == "*":
            return multiply(operation_input[1], operation_input[2])
        elif operation_input[0] == "/":
            return divide(operation_input[1], operation_input[2])
        elif operation_input[0] == "pow":
            return power(operation_input[1], operation_input[2])
        elif operation_input[0] == "mod":
            return mod(operation_input[1], operation_input[2])
    except IndexError:
        operation_input[1] = float(operation_input[1])
        if operation_input[0] == "square":
            return square(operation_input[1])
        elif operation_input[0] == "cube":
            return cube(operation_input[1])
Example #41
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 #42
0
def calculator_run():
    while True:
        input_string = input('Enter your forumula here: ')
        tokens = input_string.split(' ')

        if tokens[0] == "q":
            print('Ending session.')
            break
        if len(tokens) == 1:
            print("Please insert a valid equation.")
        elif len(tokens) > 3:
            print("You have inserted too many variables.")

        elif len(tokens) == 3:
            formula = tokens[0]
            num1 = float(tokens[1])
            num2 = float(tokens[2])
            if formula == "+":
                print(add(num1, num2))
            elif formula == "-":
                print(subtract(num1, num2))
            elif formula == "*":
                print(multiply(num1, num2))
            elif formula == "/":
                print(divide(num1, num2))
            elif formula == "mod":
                print(mod(num1, num2))
        elif len(tokens) == 2:
            formula = tokens[0]
            num1 = float(tokens[1])
            if formula == "square":
                print(square(num1))
            elif formula == "cube":
                print(cube(num1))
            elif formula == "pow":
                print(power(num1, num2))
Example #43
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 #44
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 #45
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)
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."
        # 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."
Example #48
0
while True:
  # 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)
         print "Please enter a valid expression."
         continue
     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])
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 #51
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
            print error_string %("division", "/", "two")
        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 #53
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 #54
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 #55
0
    num_inputs = len(elements)
    operand = create_operand_array(elements)


    if correct_num_inputs(operation, num_inputs):

        if operation == "+":   
            print arithmetic.add(operand)
        elif operation == "-":
            print arithmetic.subtract(operand[0], operand[1])
        elif operation == "*":   
            print arithmetic.multiply(operand)
        elif operation == "/":
            print arithmetic.divide(operand[0], operand[1])
        elif operation == "pow":
            print arithmetic.power(operand[0], operand[1])
        elif operation == "square":
            print arithmetic.square(operand[0])
        elif operation == "cube":
            print arithmetic.cube(operand[0])
        elif operation == "%":
            print arithmetic.mod(operand[0], operand[1])
        else:
            print "Not yet implemented"






Example #56
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)
    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"