Пример #1
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
Пример #2
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]))
Пример #3
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."
Пример #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."
Пример #5
0
def do_math():
    while True:
        user_input = raw_input("> ").strip()
        if user_input == "q":
            break

        # separate user input 
        input_list = user_input.split()

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

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

            if input_list[0] == "+":
                print arithmetic.add(input_list[1:])
            elif input_list[0] == "-":
                print arithmetic.subtract(input_list[1:])
            elif input_list[0] == "*":
                print arithmetic.multiply(input_list[1:])
            elif input_list[0] == "/":
                print arithmetic.divide(input_list[1:])
            elif input_list[0] in ["square", "cube"]:
                print "Oops! You gave us too many numbers!  Just give 1. Try again."
            elif input_list[0] in ["pow", "mod"]:
                # error check for too many numbers
                if len(input_list) == 3:
                    if input_list[0] == "pow":
                        print arithmetic.power(second, third)
                    elif input_list[0] == "mod":
                        print arithmetic.mod(second, third)
                else:
                    print "Oops! You gave us too many numbers!  Just give 2. Try again."
            else:
                print "I don't understand what kind of math you want me to do."
        else:
            print "I don't understand.  You said too much.  Talk less."
Пример #6
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"
Пример #7
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
Пример #8
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
Пример #9
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."
Пример #10
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."
Пример #11
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."
Пример #12
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"
Пример #13
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)
Пример #14
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)
Пример #15
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])
Пример #16
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
Пример #17
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]))
def test_divide():

    assert divide(0, 5) == 0
    assert divide(-5, 2) == -2
    assert divide(12, -2) == -6
    assert divide(-40, -6) == 6
    assert divide(12, 22) == 0
    assert divide(24, 12) == 2
Пример #19
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
Пример #20
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
Пример #21
0
def operation(operator, nums):

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

	if operator == '+':
		return arithmetic.add(x, y)
	elif operator == '-':
		return arithmetic.subtract(x, y)
	elif operator == '/':
		return "{0:.6f}".format(arithmetic.divide(float(nums[0]), float(nums[1])))
	elif operator == '*':
		return arithmetic.multiply(x, y)
	elif operator == 'square':
		return arithmetic.square(x)
	elif operator == 'cube':
		return arithmetic.cube(x)
	elif operator == 'pow':
		return arithmetic.power(x, y)
	elif operator == 'mod':
		return arithmetic.mod(x, y)
	else:
		error()
Пример #22
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
Пример #23
0
    elif len(tokens) < 2:
        print("Not enough inputs")
        break

    elif len(tokens) == 2:
        num1 = tokens[1]
        num2 = 0
    elif len(tokens) == 3:
        num1 = tokens[1]
        num2 = tokens[2]

        if operator == "+":
            result = add(float(num1), float(num2))
        elif operator == "-":
            result = subtract(float(num1), float(num2))
        elif operator == "pow":
            result = power(float(num1), float(num2))
        elif operator == "*":
            result = multiply(float(num1), float(num2))
        elif operator == "/":
            result = divide(float(num1), float(num2))
        elif operator == "sq":
            result = square(float(num1))
        elif operator == "cube":
            result = cube(float(num1))
        elif operator == "mod":
            result = mod(float(num1), float(num2))
    print(result)
#except ValueError:
Пример #24
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
Пример #25
0
def main():
    while True:
        print "Calculate!"
        
        correct_entry = False

        user_input = raw_input("> ")

        token = user_input.split(" ")   # separate different parts of operation
        
        # check  length of the [token] list to set the variables appropriately
        # make sure that token[1] and token[2] are digits
        # rename token[1] and token[2] as num1 and num2
        

        if len(token) == 1:
            pass
        elif len(token) == 2:
            if(token[1].isdigit() == False):
                print "Number should be in digit format"
            elif token[0] in ["+", "-", "*", "/","pow", "mod"]:
                print "Error: check your arguments"
            else:     
                num1 = int(token[1])
                correct_entry = True
            
        else:
            if(token[1].isdigit() == False or token[2].isdigit() == False):
                print "Number should be in digit format"
            else:
                num1 = int(token[1])
                num2 = int(token[2])
                correct_entry = True


                
        # allow user to quit the program
        if token[0] == "q":
            break
       
        if correct_entry == True:
            # begin arithmetic calculations
            success = True
            if token[0] == "+":
                output = arithmetic.add(num1, num2)
                
            elif token[0] in ["-", "sub", "subtract"]:
                output = arithmetic.subtract(num1, num2)

            elif token[0] == "*":
                output = arithmetic.multiply(num1, num2)

            elif token[0] == "/":
                if num2 == 0:
                    print "Error: You can't divide by 0, dummy."
                    success = False
                else:
                    output = arithmetic.divide(num1, num2)

            elif token[0] == "square":
                output = arithmetic.square(num1)
             
            elif token[0] == "cube":
                output = arithmetic.cube(num1)

            elif token[0] == "pow":
                output = arithmetic.power(num1, num2)

            elif token[0] == "mod":
                output = arithmetic.mod(num1, num2)
            
            else:
                print "I don't understand. Try again."
                success = False

            if success == True:
                print output        
Пример #26
0
 token = user_input.split(' ')
 if (token[0] == 'q'):
     print("You have left")
     break
 else:
     if (token[0] == '+'):
         output = add(float(token[1]), float(token[2]))
         print(output)
     if (token[0] == '-'):
         output = subtract(float(token[1]), float(token[2]))
         print(output)
     if (token[0] == '*'):
         output = multiply(float(token[1]), float(token[2]))
         print(output)
     if (token[0] == '/'):
         output = divide(token[1], float(token[2]))
         print(output)
     if (token[0] == 'square'):
         output = square(float(token[1]))
         print(output)
     if (token[0] == 'cube'):
         output = cube(float(token[1]))
         print(output)
     if (token[0] == 'pow'):
         output = power(float(token[1]), float(token[2]))
         print(output)
     if (token[0] == 'mod'):
         output = mod(float(token[1]), float(token[2]))
         print(output)
     if (token[0] == 'quit'):
         print("You have left")
Пример #27
0
def test_divdie_by_zero():
    with pytest.raises(ZeroDivisionError):
        arithmetic.divide(3, 0)
Пример #28
0
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])
		print result
	elif token[0] == 'mod':
		result = mod(token[1], token[2])
		print result
	else:
		print "I don't know that one"
Пример #29
0
#     return len(tokens)

# Your code goes here
while True:
    try:
        arithmetic_operation = input(">")
        tokens = arithmetic_operation.split(" ")
        if tokens[0] == "q":
            break
        token_one = int(tokens[1])
        if len(tokens) > 2:
            token_two = int(tokens[2])
            if tokens[0] == "+":
                print(add(token_one, token_two))
            elif tokens[0] == "-":
                print(subtract(token_one, token_two))
            elif tokens[0] == "*":
                print(multiply(token_one, token_two))
            elif tokens[0] == "/":
                print(divide(token_one, token_two))
            elif tokens[0] == "mod":
                print(mod(token_one, token_two))
            elif tokens[0] == "pow":
                print(power(token_one, token_two))
        elif tokens[0] == "square":
            print(square(token_one))
        elif tokens[0] == "cube":
            print(cube(token_one))
    except IndexError:
        print("invalid token")
Пример #30
0
while calculator == True:
    input_string = input('>')
    token = input_string.split(' ')
    if token[0] == 'q':
        calculator = False
    else:
        a = int(token[1])
        if token[0] == '+':
            b = int(token[2])
            x = add(a, b)
        elif token[0] == '-':
            b = int(token[2])
            x = subtract(a, b)
        elif token[0] == '*':
            b = int(token[2])
            x = multiply(a, b)
        elif token[0] == '/':
            b = int(token[2])
            x = divide(a, b)
        elif token[0] == 'square':
            x = square(a)
        elif token[0] == 'cube':
            x = cube(a)
        elif token[0] == 'pow':
            b = int(token[2])
            x = power(a, b)
        elif token[0] == 'mod':
            b = int(token[2])
            x = mod(a, b)
        print(x)
    elif operator == '*':
        if num_args < 2:
            print error_string %("multiplication", "*", "two or more")
        else: 
            ans = 1
            for n in nums:
                ans = art.multiply(ans, n)
            print ans

    elif operator == '/':
        if num_args != 2:
            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:
Пример #32
0
        # Evaluate input for validity
        try:
            if type(tokens[0]) is str and type(int(tokens[1])) is int and type(
                    int(tokens[2])) is int:
                # Reinitialize validity to be true
                input_validity = True

                # Call pow function if first token matches 'pow'
                if tokens[0] == 'pow':
                    print(power(int(tokens[1]), int(tokens[2])))
                # Call add function if first token matches '+'
                if tokens[0] == '+':
                    print(add(int(tokens[1]), int(tokens[2])))
                # Call divide function if first token matches '/'
                if tokens[0] == '/':
                    print(divide(int(tokens[1]), int(tokens[2])))
                # Call subtract function if first token matches '-'
                if tokens[0] == '-':
                    print(subtract(int(tokens[1]), int(tokens[2])))
                # Call multiply function if first token matches '*'
                if tokens[0] == '*':
                    print(multiply(int(tokens[1]), int(tokens[2])))
                # Call square function if first token matches 'square'
                if tokens[0] == 'square':
                    print(square(int(tokens[1])))
                # Call cube function if first token matches 'cube'
                if tokens[0] == 'cube':
                    print(cube(int(tokens[1])))
                # Call mod function if first token matches 'mod'
                if tokens[0] == 'mod':
                    print(mod(int(tokens[1]), int(tokens[2])))
Пример #33
0
# Look for a quit condition and break
    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"
Пример #34
0
         token[1] = int(token[1])
     except ValueError:
         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])
Пример #35
0
     break
 elif len(tokens) == 2:
     tokens[1] = int(tokens[1])
     if tokens[0] == "square":
         result = square(tokens[1])
     elif tokens[0] == "cube":
         result = cube(tokens[1])
 elif len(tokens) == 3:
     tokens[1] = int(tokens[1])
     tokens[2] = int(tokens[2])
     if tokens[0] == "+":
         result = add(tokens[1], tokens[2])
     elif tokens[0] == "-":
         result = subtract(tokens[1], tokens[2])
     elif tokens[0] == "*":
         result = multiply(tokens[1], tokens[2])
     elif tokens[0] == "/":
         result = divide(tokens[1], tokens[2])
     elif tokens[0] == "pow":
         result = power(tokens[1], tokens[2])
     elif tokens[0] == "mod":
         result = mod(tokens[1], tokens[2])
     elif tokens[0] == "cubes+":
         result = add_cubes(tokens[1], tokens[2])
 elif len(tokens) == 4:
     tokens[1] = int(tokens[1])
     tokens[2] = int(tokens[2])
     tokens[3] = int(tokens[3])
     if tokens[0] == "x+":
         result = add_mult(tokens[1], tokens[2], tokens[3])
 print(result)
Пример #36
0
        #print error message
        #else:
        #try to convert the passed string to integer
        #exception would be a print statement with an error message

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

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

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

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

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

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

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

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

        print(float(answer))
Пример #37
0
        break
    else:
        for index in range(1, len(exp_tokens)):
            exp_tokens[index] = int(exp_tokens[index])

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

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

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

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

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

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

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

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

        else:
            answer = 'Invalid expression.'
Пример #38
0
def main():
  
    while True:
        calculation = raw_input(">")
        calc_parts = calculation.split(" ")
        first_input = calc_parts[0]
        valid_one_num_operators = ["square", "cube"]
        valid_two_num_operators = ["+", "-", "*", "/", "pow", "mod"]

        # determine whether they want to quit
        if len(calc_parts) == 1:
            if first_input == "q":
                break
            else:
                print "I don't understand."
                continue

        elif len(calc_parts) == 2:
            if (first_input in valid_one_num_operators) and check_for_valid_number(calc_parts[1]):
                num1 = int(calc_parts[1])
            else:
                print "I don't understand."
                continue

        elif len(calc_parts) == 3:
            if (first_input in valid_two_num_operators) and check_for_valid_number(calc_parts[1]) and check_for_valid_number(calc_parts[2]):
                num1 = int(calc_parts[1])
                num2 = int(calc_parts[2])
            else:
                print "I don't understand."
                continue
        else:
            print "I don't understand."
            continue


        # decide which math function to call
        if first_input == "+":
            print arithmetic.add(num1, num2)

        elif first_input == "-":
            print arithmetic.subtract(num1, num2)

        elif first_input == "*":
            print arithmetic.multiply(num1, num2)

        elif first_input == "/":
            print arithmetic.divide(num1, num2)

        elif first_input == "square":
            print arithmetic.square(num1)

        elif first_input == "cube":
            print arithmetic.cube(num1)

        elif first_input == "pow":
            print arithmetic.power(num1, num2)

        elif first_input == "mod":
            print arithmetic.mod(num1, num2)

        else:
            print "I don't understand."
Пример #39
0
    num1 = float(token[1])
    num2 = float(token[2])

    result = None

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

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

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

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

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

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

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

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

    print(result)
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."
Пример #41
0
def test_divide():
    assert arithmetic.divide(4, 3) == 
Пример #42
0
def main():
    # Your code goes here
    quit_calculator = False
    while quit_calculator == False:
        user_input = raw_input('> ')
        calculator_input = user_input.split(" ")
        user_math_operator = calculator_input[0]

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

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

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

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

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

        else:
            return 0
Пример #43
0
    for num in inputs:
        tokens.append(float(num))

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

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

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

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

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

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

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

        elif operator == 'mod':
            print(mod(float(tokens[1]), float(tokens[2])))
    except:
        print("invalid input")
Пример #44
0
    # elif len(tokens) < 3:
    #     num2 = '0'
    # else:
    #     num2 = tokens[2]

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

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

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

    print(result)
Пример #45
0
import arithmetic

print arithmetic.add(5, 8)
print arithmetic.subtract(10, 5)
print arithmetic.multiply(12, 6)
print arithmetic.divide(6, 2)
print arithmetic.divide(6, 0)
Пример #46
0
    divide,
    square,
    cube,
    power,
    mod,
)

while True:
    calculation = input("What would you like to calculate?")
    toke_calculation = calculation.split(' ')
    print(toke_calculation)
    if toke_calculation[0] == "q":
        break
    result = None
    if toke_calculation[0] == "+":
        result = add(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "-":
        result = subtract(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "*":
        result = multiply(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "/":
        result = divide(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "square":
        result = square(int(toke_calculation[1]))
    elif toke_calculation[0] == "cube":
        result = cube(int(toke_calculation[1]))
    elif toke_calculation[0] == "power":
        result = power(int(toke_calculation[1]), int(toke_calculation[2]))
    elif toke_calculation[0] == "mod":
        result = mod(int(toke_calculation[1]), int(toke_calculation[2]))
    print(result)
Пример #47
0
def calculator():

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

        tokens_from_user_input = user_input.split(' ')

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

        if tokens_from_user_input[0] == "+":

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

            print(add(num1, num2))

        if tokens_from_user_input[0] == "-":

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

            print(subtract(num1, num2))

        if tokens_from_user_input[0] == "*":

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

            print(multiply(num1, num2))

        if tokens_from_user_input[0] == "/":

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

            print(divide(num1, num2))

        if tokens_from_user_input[0] == "square":

            num1 = int(tokens_from_user_input[1])

            print(square(num1))

        if tokens_from_user_input[0] == "cube":

            num1 = int(tokens_from_user_input[1])

            print(cube(num1))

        if tokens_from_user_input[0] == "pow":

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

            print(power(num1, num2))

        if tokens_from_user_input[0] == "mod":

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

            print(mod(num1, num2))
Пример #48
0
        tokenized_input = user_expression.split(' ')

        for i in range(1, len(tokenized_input)):
            tokenized_input[i] = int(tokenized_input[i])

        if tokenized_input[0] == "+":
            result = add(tokenized_input[1], tokenized_input[2])

        elif tokenized_input[0] == "-":
            result = subtract(tokenized_input[1], tokenized_input[2])

        elif tokenized_input[0] == "*":
            result = multiply(tokenized_input[1], tokenized_input[2])

        elif tokenized_input[0] == "/":
            result = divide(tokenized_input[1], tokenized_input[2])

        elif tokenized_input[0] == "square":
            result = square(tokenized_input[1])

        elif tokenized_input[0] == "cube":
            result = cube(tokenized_input[1])

        elif tokenized_input[0] == "pow":
            result = power(tokenized_input[1], tokenized_input[2])

        elif tokenized_input[0] == "mod":
            result = mod(tokenized_input[1], tokenized_input[2])

        elif tokenized_input[0] == "x+":
            result = add_mult(tokenized_input[1], tokenized_input[2],
Пример #49
0
        print("Not enough inputs.")
    if len(tokens) == 2 or len(tokens) == 3:
        num1 = float(tokens[1])
    if len(tokens) == 3:
        num2 = float(tokens[2])
    if len(tokens) > 3:
        print("Too many inputs.")

    if operator == '+':
        print(add(num1, num2))
    elif operator == '-':
        print(subtract(num1, num2))
    elif operator == '*':
        print(multiply(num1, num2))
    elif operator == '/':
        print(divide(num1, num2))
    elif operator == 'square':
        if len(tokens) > 2:
            print("Too many inputs.")
        else:
            print(square(num1))
    elif operator == 'power':
        print(power(num1, num2))
    elif operator == 'cube':
        if len(tokens) > 2:
            print("Too many inputs.")
        else:
            print(cube(num1))
    elif operator == 'mod':
        print(mod(num1, num2))
    else:
Пример #50
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)
Пример #51
0
        if len(input_list) > 2:
            try:
                num2 = float(input_list[2])
            except ValueError as e:
                print("Invalid input. Please enter a number.")
                continue


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

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

        print(solution)
        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)
        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)
while True:
    user_input = input("> ")
    tokens = user_input.split(" ")
    numbers = []
    for each in tokens[1:]:
        each = float(each)
        numbers.append(each)

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

# Replace this with your code
Пример #54
0
    if "q" in token:
        print("calculator shutting down...")
        break

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

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

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

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

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

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

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

    print(answer)
Пример #55
0
    tokens = user_input.split(" ")
    operator = tokens[0]
    number1 = float(tokens[1])
    number2 = float(tokens[2])
    if len(tokens) < 2:
        print("Not enough inputs.")
        continue

    elif operator == "add":
        print(add(number1,number2))

    elif operator == "subtract":
        print(subtract(number1,number2))

    elif operator == "multiply":
        print(multiply(number1,number2))

    elif operator == "divide":
        print(divide(number1,number2))

    elif operator == "square":
        print(square(number1,number2))

    elif operator == "cube":
        print(cube(number1,number2))

    elif operator == "power":
        print(power(number1,number2))

    elif operator == "mod":
        print(mod(number1,number2))
Пример #56
0
        print("please enter a valid operand.")
        continue
    elif query[0]=="q" or query[0]=="quit":
        break
    else:
        try:
            if len(query[1:]) != 2 and query[0] in ["+", "-", "*", "/", "pow"]:
                print("the +, -, *, /, pow, and mod functions all take exactly 2 arguments following the operand.")
            elif len(query[1:]) != 1 and query[0] in ["square", "cube"]:
                print("the square and cube functions both take exactly one argument following the operand.")
            else:
                if "+" in query[0]:
                    print(float(query[1])+float(query[2]))
                if "-" in query[0]:
                    print(float(query[1])-float(query[2]))
                if "*" in query[0]:
                    print(float(query[1])*float(query[2]))
                if "/" in query[0]:
                    print(arithmetic.divide(query[1], query[2]))
                if "pow" in query[0]:
                    print(float(query[1])**float(query[2]))
                if "mod" in query[0]:
                    print(float(query[1])%float(query[2]))
                if "square" in query[0]:
                    print(float(query[1])**2)
                if "cube" in query[0]:
                    print(float(query[1])**3)
            

        except ValueError:
            print("please enter valid arguments.")
Пример #57
0
                num.append(float(tokens[i]))
        except ValueError:
            print("\n One or more of the value(s) provided is not a number")
            continue

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

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

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

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

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

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

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

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

        elif (tokens[0] == "add_mult"):
            print(add_mult(num))
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."
Пример #59
0
    #print(token)

    if (token[0] == "q"):
        break
    else:
        if (token[0] == 'add'):
            result = add(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'subtract'):
            result = subtract(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'multiply'):
            result = multiply(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'divide'):
            result = divide(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'square'):
            result = square(int(token[1]))
            print(result)
        elif (token[0] == 'cube'):
            result = cube(int(token[1]))
            print(result)
        elif (token[0] == 'power'):
            result = power(int(token[1]), int(token[2]))
            print(result)
        elif (token[0] == 'mod'):
            result = mod(int(token[1]), int(token[2]))
            print(result)
        else:
            break
Пример #60
0
            elif (tokens[0] != 'square' and tokens[0] != 'cube' and len(tokens) < 3) or len(tokens) <= 1:
                print('You need more inputs. Please try again.')
            else: break # if first token is square or cube and num1 is valid
        except ValueError:
            print('That is not valid input. Please try again.')
            print('Your entry should be of the format "operator number_1 (number_2)"')

    # perform the appropriate task
    if tokens[0] == '+':
        result = add(num1, num2)
    elif tokens[0] == '-':
         result = subtract(num1, num2)
    elif tokens[0] == '*':
        result = multiply(num1, num2)
    elif tokens[0] == '/':
        result = divide(num1, num2)
    elif tokens[0] == 'square':
        result = square(num1)
    elif tokens[0] == 'cube':
        result = cube(num1)
    elif tokens[0] == 'pow' or tokens[0] == 'power':
        result = power(num1, num2)
    elif tokens[0] == 'mod' or tokens[0] == '%':
        result = int(mod(num1, num2)) # result should be int!
    elif tokens[0] == 'q':
        result = 'Okay, goodbye!'
    else:
        result = 'That is not valid operator. Please try again. \nYour options are +, -, *, /, square, cube, pow, mod'
    print(result)