Exemplo n.º 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."
Exemplo n.º 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."
Exemplo n.º 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
Exemplo n.º 4
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]))
Exemplo n.º 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]))
Exemplo n.º 6
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."
Exemplo n.º 7
0
def test_subtract():
    assert arithmetic.subtract(1, 1) == 0
    assert arithmetic.subtract(2, 0.5) == 1.5
    assert arithmetic.subtract(0, -1) == 1

    x = random.random()
    y = random.random()
    assert arithmetic.subtract(x, y) == approx(x - y)
Exemplo n.º 8
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."
Exemplo n.º 9
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."
Exemplo n.º 10
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()

    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"
Exemplo n.º 11
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])))
Exemplo n.º 12
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."
Exemplo n.º 13
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])))
Exemplo n.º 14
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
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
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."
Exemplo n.º 18
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."
Exemplo n.º 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")
Exemplo n.º 20
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))
Exemplo n.º 21
0
def calculate_numbers():

    while True:        
        answer = input("Enter your string > ")
        tokens = answer.split(' ')
        operator = tokens[0]
        num1 = float(tokens[1])
        num2 = float(tokens[2])
        if operator == 'q':
            print("Game over")
            break
        else:
            if operator == 'add':
                add(num1, num2)
            elif operator == 'sub':
                subtract(num1, num2)
Exemplo n.º 22
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)))
Exemplo n.º 23
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!")
Exemplo n.º 24
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
Exemplo n.º 25
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])))
Exemplo n.º 26
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
Exemplo n.º 27
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"
Exemplo n.º 28
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()

    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"
Exemplo n.º 29
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]))
Exemplo n.º 30
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)
Exemplo n.º 31
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)
Exemplo n.º 32
0
def operation3(tokens):
    operator = tokens[0]

    if operator == "+":
        result = arithmetic.add(tokens[1:])
    elif operator == "-":
        result = arithmetic.subtract(tokens[1:])
    else:
        result = arithmetic.multiply(tokens[1:])

    print result
Exemplo n.º 33
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')
Exemplo n.º 34
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])
Exemplo n.º 35
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]))
Exemplo n.º 36
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
Exemplo n.º 37
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."
Exemplo n.º 38
0
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))
Exemplo n.º 39
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
Exemplo n.º 40
0
    def calculate(initial_string):
        if initial_string == "q":
            exit()

        else:
            split_string = initial_string.split(" ")

            num1 = int(split_string[1])
            num2 = 0

            if len(split_string) > 2:
                num2 = int(split_string[2])

            #consideration to make dictionary for operands
            """(add, subtract, multiply, divide, square, cube,
                            power, mod, )
                            """
            if split_string[0] == '+':
                print(add(num1, num2))

            elif split_string[0] == '-':
                print(subtract(num1, num2))

            elif split_string[0] == '*':
                print(multiply(num1, num2))

            elif split_string[0] == '/':
                print(divide(num1, num2))

            elif split_string[0] == 'square':
                print(square(num1))

            elif split_string[0] == 'cube':
                print(cube(num1))

            elif split_string[0] == 'pow':
                print(pow(num1, num2))

            elif split_string[0] == 'mod':
                print(mod(int(num1), int(num2)))
Exemplo n.º 41
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.')
Exemplo n.º 42
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!"
Exemplo n.º 43
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
Exemplo n.º 44
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."
Exemplo n.º 45
0
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)
Exemplo n.º 46
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
Exemplo n.º 47
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])
Exemplo n.º 48
0
def operate(operator, nums_list):
    equation = None
    if operator == '+':
        equation = add(nums_list[0], nums_list[1])

    elif operator == '-':
        equation = subtract(nums_list[1], nums_list[2])

    elif operator == '*':
        equation = multiply(nums_list[0], nums_list[1])

    elif operator == '/':
        equation = divide(nums_list[0], nums_list[1])

    elif operator == 'square':
        equation = square(nums_list[0])

    elif operator == 'cube':
        equation = cube(nums_list[1])

    elif operator == 'mod':
        equation = mod(nums_list[0], nums_list[1])

    print("result is {}".format(equation))
Exemplo n.º 49
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()
Exemplo n.º 50
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))
    user_input = raw_input("> ")  # requests user to enter an expression
    tokens = user_input.split(" ")  # separates the user's imput into a list

    if tokens[0] == "q":
        break

    elif tokens[0] == "+":
        # print int(tokens[1]) + int(tokens[2])
        tokens = makeInt(tokens)
        print arithmetic.add(tokens[1], tokens[2])

    elif tokens[0] == "-":
        # tokens[1] = int(tokens[1]) # converted this into the makeInt function
        # 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)
Exemplo n.º 52
0
import arithmetic
print arithmetic.add(5, 8)
print arithmetic.subtract(10, 5)
print arithmetic.multiply(12, 6)
Exemplo n.º 53
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)
Exemplo n.º 54
0
import arithmetic
 
print (arithmetic.add(5, 8))
print (arithmetic.subtract(10, 5))
print (arithmetic.division(2, 7))
print (arithmetic.multiply(12, 6))
Exemplo n.º 55
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
Exemplo n.º 56
0
from arithmetic import divide
from arithmetic import square
from arithmetic import cube
from arithmetic import power
from arithmetic import mod

while 1:
	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])
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."