Exemplo n.º 1
0
def main():
    root = make_root()
    display = make_display(root)
    label = make_label(root)
    buttons = make_buttons(root)
    calculator = Calculator(root, label, display, buttons)
    calculator.start()
 def test_fib_gen(self):
     results = [0, 1, 1, 2, 3, 5]
     lst = [5, 6]
     fibo = Calculator.fib_gen(lst)
     for i in range(lst[0]):
         self.assertEqual(results[i], next(fibo))
         
     for i in range(lst[1]):
         self.assertEqual(results[i], next(fibo))
Exemplo n.º 3
0
def process_operation():
    loop_back = 'y'
    while loop_back == 'y':

        # user input to select operation type
        print("\nSelect operation")
        print("1. Add two lists")
        print("2. Add two lists the Guido van Rossum's way")
        print("3. Even numbers in a list")
        print("4. Even numbers in a list the Guido van Rossum's way")
        print("5. Odd numbers in a list")
        print("6. Odd numbers in a list the Guido van Rossum's way")
        print("7. Max value in a list")
        print("8. Min value in a list")
        print("9. Fibonacci of numbers in a list using recursion")
        print(
            "10. Fibonacci of numbers in a list using recursion and memorisation"
        )
        print("11. Fibonacci sequence generator up to n elements")
        print(
            "12. Square generator of a list of numbers the Guido van Rossum's way"
        )

        try:
            choice = int(input("\nEnter choice: "))

            if choice in range(1, 3):
                try:
                    lst1 = [
                        eval(num) for num in input(
                            "Enter the numbers of the first list separated by space: "
                        ).strip().split()
                    ]
                    lst2 = [
                        eval(num) for num in input(
                            "Enter the numbers of the second list separated by space: "
                        ).strip().split()
                    ]

                    if choice == 1:
                        print("\nThe added values are: ",
                              Calculator.add(lst1, lst2))

                    elif choice == 2:
                        print("\nThe added values are: ",
                              Calculator.add_lst_comp(lst1, lst2))

                except (NameError, SyntaxError, TypeError):
                    print("\nWarning: only enter numbers")

            elif choice in range(2, 13):
                try:
                    lst = [
                        eval(num) for num in input(
                            "Enter the list numbers separated by space: ").
                        strip().split()
                    ]

                    if choice == 3:
                        print("\nThe even numbers are: ", Calculator.even(lst))

                    elif choice == 4:
                        print("\nThe even numbers are: ",
                              Calculator.even_lst_comp(lst))

                    elif choice == 5:
                        print("\nThe odd numbers are: ", Calculator.odd(lst))

                    elif choice == 6:
                        print("\nThe odd numbers are: ",
                              Calculator.odd_lst_comp(lst))

                    elif choice == 7:
                        print("\nThe maximum value is: ",
                              Calculator.max_value(lst))

                    elif choice == 8:
                        print("\nThe minimum value is: ",
                              Calculator.min_value(lst))

                    elif choice == 9:
                        print("\nThe fibonacci numbers are: ",
                              Calculator.fib_recursion(lst))

                    elif choice == 10:
                        print("\nThe fibonacci numbers are: ",
                              Calculator.fib_recursion_memo(lst))

                    elif choice == 11:
                        fibonacci = Calculator.fib_gen(lst)
                        answer = 'y'
                        while answer == 'y':
                            try:
                                print("\nThe next fibonacci is",
                                      next(fibonacci))
                                answer = input(
                                    "Do you want to see the next fibonacci'y/n'? "
                                )

                            except StopIteration:
                                print(
                                    "\nYou have now seen all fibonacci numbers"
                                )
                                answer = input(
                                    "Enter 'n' to exit fibonacci numbers: ")

                    elif choice == 12:
                        square = Calculator.square_gen_comp(lst)
                        answer = 'y'
                        while answer == 'y':
                            try:
                                print("\nThe next square is", next(square))
                                answer = input(
                                    "Do you want to see the next square number'y/n'? "
                                )

                            except StopIteration:
                                print("\nYou have now seen all square numbers")
                                answer = input(
                                    "Enter 'n' to exit square numbers: ")

                except (NameError, SyntaxError, TypeError):
                    print("\nWarning: only enter numbers")

        # Any other choice that is not in the expected list
        except ValueError:
            print("\nWarning: choice must be an integer between 1 and 12")

        # user input for loop_back
        loop_back = input("\nDo you want to compute another operation 'y/n'? ")
Exemplo n.º 4
0
from calculator_class import Calculator

calculator_object = Calculator()

a = float(input('What number would you like to convert to inches'))
inches = round(calculator_object.multiply(a, 2.54), 2)

print(f' {a} cm = {inches} inches (to 2 decimal places).')
Exemplo n.º 5
0
from calculator_class import Calculator # import the class that handles the calculator functions

c = Calculator() # create a Calculator object
ans = None
operators = '** * / // - +'.split() # list of available operators for use in the calculator
evaluated = False

"""This calculator is intended to handle most expressions 
available on a standard calculator, while handling user
errors at the same time, such as invalid expressions or 
zero division errors. The calculator will run until the
user types "exit", and the user has the option of carrying over
their answer to perform more calculations, or clearing their 
answer and starting over again. In addition, several blank
print lines were added to enhance readability and ensure that
lines are not placed too closely together."""

print('Please enter only one equation at a time. If you have a bigger equation, please consider the order of operations, then enter multiple equations.\n')
while ans != 'exit':
    ans = input('Please enter the equation you want to evaluate, one character at a time. If you want to stop, type "exit": ').lower().strip()

    try:       # try the below, and if it fails, move to the except blocks to catch errors.
        if ans == '=':
            result = c.equals()
            print(result)
            evaluated = True

        elif ans.isdecimal() or ans in operators:
            c.push(ans)     # push the answer onto the expression, which is separated by a space in the method definition.
            
        else:
 def test_square_gen_comp(self):
     lst = [-2, 3, 4, 5]
     results = [4, 9, 16, 25]
     square = Calculator.square_gen_comp(lst)
     for i in lst:
         self.assertEqual(results[lst.index(i)], next(square))
 def test_min_value(self):
     self.assertEqual(1, Calculator.min_value(self.lst1))
     self.assertEqual(5, Calculator.min_value(self.lst2))
 def test_fib_recursion_memo(self):
     self.assertEqual([1, 1, 2, 3], Calculator.fib_recursion_memo(self.lst1))
     self.assertEqual([5, 8, 13, 21], Calculator.fib_recursion_memo(self.lst2))
     self.assertRaises(RecursionError, Calculator.fib_recursion_memo(self.lst3))
 def test_max_value(self):
     self.assertEqual(4, Calculator.max_value(self.lst1))
     self.assertEqual(8, Calculator.max_value(self.lst2))
 def test_odd_lst_comp(self):
     self.assertEqual([1, 3], Calculator.odd_lst_comp(self.lst1))
     self.assertEqual([5, 7], Calculator.odd_lst_comp(self.lst2))
 def test_even_lst_comp(self):
     self.assertEqual([2, 4], Calculator.even_lst_comp(self.lst1))
     self.assertEqual([6, 8], Calculator.even_lst_comp(self.lst2))
 def test_add_lst_comp(self):
     self.assertEqual([6, 8, 10, 12], Calculator.add_lst_comp(self.lst1, self.lst2))
     self.assertEqual([-4, 8, 10], Calculator.add_lst_comp(self.lst1, self.lst3))
     self.assertEqual([-4, 8, 10], Calculator.add_lst_comp(self.lst3, self.lst1))
 def test_add(self):
     self.assertEqual([6, 8, 10, 12], Calculator.add(self.lst1, self.lst2))