示例#1
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."
示例#2
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
示例#3
0
def calculator(input):

    token = input.split(" ")

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

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

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

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

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

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

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

    if token[0] == "mod":
        print arithmetic.mod(int(token[1]), int(token[2]))
示例#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]))
示例#5
0
文件: calculator.py 项目: jgrist/ex03
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."
示例#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."
示例#7
0
def two_count_functions(token_list):
    # run two count functions        
    if token_list[0] == "square":
        print arithmetic.square(token_list[1])
    elif token_list[0] == "cube":
        print arithmetic.cube(token_list[1])
    else:
        print "Two few functions. Input again."
示例#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."
示例#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."
示例#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"
示例#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])))
示例#12
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!")
示例#13
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])))
示例#14
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)))
示例#15
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])))
示例#16
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
示例#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."
示例#18
0
def calculator(input_string):
    """creates a calculator to reference functions in arithmetic.py"""
    #statement = input("Enter your arithmetic argument: ")
    statement = input_string.split(" ")
    first_token = statement[0]
    second_token = int(statement[1])
    third_token = int(statement[2])
    if first_token == "q":
        quit
    else:
        if first_token == "+":
            output = add(second_token, third_token)
        elif first_token == "-":
            output = subtract(second_token, third_token)
        elif first_token == "/":
            output = divide(second_token, third_token)
        elif first_token == "square":
            output = square(second_token, third_token)
        elif first_token == "cube":
            output = cube(second_token, third_token)
        elif first_token == "pow":
            output = power(second_token, third_token)
        elif first_token == "mod":
            output = mod(second_token, third_token)
        print(output)
        return output
示例#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")
示例#20
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
示例#21
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."
示例#22
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))
示例#23
0
def main_calculator():
    while True:
        input_ = input()
        tokenized_input = input_.split(' ')
        tokenized_input = [tokenized_input[0]
                           ] + [int(string) for string in tokenized_input[1:]]
        if tokenized_input[0] == 'q':
            break
        else:
            if tokenized_input[0] == '+':
                print(reduce(add, tokenized_input[1:]))
            if tokenized_input[0] == '+':
                print(reduce(add, tokenized_input[1:]))
            if tokenized_input[0] == '-':
                print(reduce(subtract, tokenized_input[1:]))
            if tokenized_input[0] == '*':
                print(reduce(multiply, tokenized_input[1:]))
            if tokenized_input[0] == '/':
                print(reduce(divide, tokenized_input[1:]))
            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(reduce(power, tokenized_input[1:]))
            if tokenized_input[0] == 'mod':
                print(reduce(mod, tokenized_input[1:]))
示例#24
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"
示例#25
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]))
示例#26
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"
示例#27
0
def operation1(tokens):
    operator = tokens[0]
    num  = int(tokens[1])

    if operator == "square":
        result = arithmetic.square(num)
    else:
        result = arithmetic.cube(num)

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

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


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

            elif user_math_operator == "q":
                quit_calculator = True

        else:
            return 0
def 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))
示例#32
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
示例#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')
示例#34
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)))
示例#35
0
def calculator():
    while True:
        try:
            read_input = input("Enter your equation: ")
            tokens = read_input.split(" ")

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

            else:
                if tokens[0] == "+":  
                    answer = add(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "-":
                    answer = subtract(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "*":
                    answer = multiply(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "/":
                    answer = divide(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "square":
                    answer = square(float(tokens[1]))
                    print(answer)
                elif tokens[0] == "cube":
                    answer = cube(float(tokens[1]))
                    print(answer)
                elif tokens[0] == "pow":
                    answer = power(float(tokens[1]), float(tokens[2]))
                    print(answer)
                elif tokens[0] == "mod":
                    answer = mod(float(tokens[1]), float(tokens[2]))
                    print(answer)
                else:
                    print("Please type in an operator and 2 numbers separated by spaces.")
        except ValueError:
            print('That is not a number.')
示例#36
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
示例#37
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!"
示例#38
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."
示例#39
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)
示例#40
0
def arithmetic_stuff(user_input_from_func):

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

    if operator == 'add':
        print(add(num1, num2))
    elif operator == 'subtract':
        print(subtract(num1, num2))
    elif operator == 'multiply':
        print(multiply(num1, num2))
    elif operator == 'divide':
        print(divide(num1, num2))
    elif operator == 'square':
        print(square(num1))
    elif operator == 'cube':
        print(cube(num1))
    elif operator == 'power':
        print(power(num1, num2))
    elif operator == 'mod':
        print(mod(num1, num2))
    '''our add_mult and add_cubes are not working, program returns error of 'add_mult' is not defined'''
示例#41
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()
示例#42
0
def calculator_run():
    while True:
        input_string = input('Enter your forumula here: ')
        tokens = input_string.split(' ')

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

        elif len(tokens) == 3:
            formula = tokens[0]
            num1 = float(tokens[1])
            num2 = float(tokens[2])
            if formula == "+":
                print(add(num1, num2))
            elif formula == "-":
                print(subtract(num1, num2))
            elif formula == "*":
                print(multiply(num1, num2))
            elif formula == "/":
                print(divide(num1, num2))
            elif formula == "mod":
                print(mod(num1, num2))
        elif len(tokens) == 2:
            formula = tokens[0]
            num1 = float(tokens[1])
            if formula == "square":
                print(square(num1))
            elif formula == "cube":
                print(cube(num1))
            elif formula == "pow":
                print(power(num1, num2))
示例#43
0
def calculator(input_string):
    """Reads a keyboard input string that is space delimited 
       and prints the result of the called function.
    """

    hell = 'warm'
    while not hell == 'frozen':
        string_as_list = input_string.split(' ')
        if string_as_list[0] == 'q':
            hell = "frozen"
        elif string_as_list[0] == 'pow':
            print(power(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '+':
            print(add(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '-':
            print(subtract(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '/':
            print(divide(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == 'square':
            print(square(float(string_as_list[1])))
            input_string = input('???')
        elif string_as_list[0] == 'cube':
            print(cube(float(string_as_list[1])))
            input_string = input('???')
        elif string_as_list[0] == 'mod':
            print(mod(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        elif string_as_list[0] == '*':
            print(multiply(float(string_as_list[1]), float(string_as_list[2])))
            input_string = input('???')
        else:
            input_string = input('Please provide relevant arguments.')
示例#44
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
示例#45
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])
示例#46
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))
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."
示例#48
0
'q'.
"""

import arithmetic as math

result = 0

while True:
    calc_input = raw_input()
    token = calc_input.split(" ")

    if len(token) == 1:
        if token[0] == "q":
            break
        elif token[0] == "cube":
            result = math.cube(result)
        elif token[0] == "square":
            result = math.square(result)

    elif len(token) == 2:
        try:
            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])
示例#49
0
    num_inputs = len(elements)
    operand = create_operand_array(elements)


    if correct_num_inputs(operation, num_inputs):

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






        # tokens[2] = int(tokens[2])
        tokens = makeInt(tokens)
        print arithmetic.subtract(tokens[1], tokens[2])

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

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

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

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

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

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

    else:
        print "I don't know what that means."
    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"
示例#52
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)
示例#53
0
    if (
        calc[0] == "+" or
        calc[0] == "-" or
        calc[0] == "*" or
        calc[0] == "/" or
        calc[0] == "pow" or
        calc[0] == "mod"):

        if (len(calc) >= 3 and
            str(calc[1]).isdigit() == True and
            str(calc[2]).isdigit() == True):

            basic(calc[0], calc[1], calc[2])
        
        else:
            print "Please provide two numbers to calculate."

    elif calc[0] == "square":
        print arithmetic.square(int(calc[1]))

    elif calc[0] == "cube":
        print arithmetic.cube(int(calc[1]))

    elif calc[0] == "q":
        break

    else:
        print "That's not a valid function. Valid functions are \
+, -, *, /, mod, square, cube, pow, or q."

            print error_string %("division", "/", "two")
        elif nums[1] == 0:
            print "Cannot divide by zero!"
        else: 
            print art.divide(nums[0], nums[1])

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

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

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

    elif operator == 'mod':
        if num_args != 2:
            print error_string %("mod", "mod", "two")
        elif nums[1] == 0:
            print "Cannot divide by zero!"
        else: 
            print art.mod(nums[0],nums[1])
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."
示例#56
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
示例#57
0
	my_input = raw_input("> ")
	token = my_input.split(" ")
	if token[0] == 'q':
		break
	elif token[0] == 'add':
		result = add(token[1], token[2])
		print result
	elif token[0] == 'subtract':
		result = subtract(token[1], token[2])
		print result
	elif token[0] == 'multiply':
		result = multiply(token[1], token[2])
		print result
	elif token[0] == 'divide':
		result = divide(token[1], token[2])
		print result
	elif token[0] == 'square':
		result = square(token[1])
		print result
	elif token[0] == 'cube':
		result = cube(token[1])
		print result
	elif token[0] == 'power':
		result = power(token[1], token[2])
		print result
	elif token[0] == 'mod':
		result = mod(token[1], token[2])
		print result
	else:
		print "I don't know that one"