Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 compatibility #1

Open
LangInteger opened this issue Feb 29, 2020 · 0 comments
Open

Python3 compatibility #1

LangInteger opened this issue Feb 29, 2020 · 0 comments

Comments

@LangInteger
Copy link

In file CS61A/lab02/lab02_extra.py / , code

def cycle(f1, f2, f3):
    """Returns a function that is itself a higher-order function.
    >>> def add1(x):
    ...     return x + 1
    >>> def times2(x):
    ...     return x * 2
    >>> def add3(x):
    ...     return x + 3
    >>> my_cycle = cycle(add1, times2, add3)
    >>> identity = my_cycle(0)
    >>> identity(5)
    5
    >>> add_one_then_double = my_cycle(2)
    >>> add_one_then_double(1)
    4
    >>> do_all_functions = my_cycle(3)
    >>> do_all_functions(2)
    9
    >>> do_more_than_a_cycle = my_cycle(4)
    >>> do_more_than_a_cycle(2)
    10
    >>> do_two_cycles = my_cycle(6)
    >>> do_two_cycles(1)
    19
    """
    "*** YOUR CODE HERE ***"
    def func(n):
        def fun(x):
            te = x
            m = 1
            while m <= n:
                if m % 3 == 1:
                    te = f1(te)
                elif m % 3 == 2:
                    te = f2(te)
                else:
                    te = f3(te)
                m += 1
            return te
        return fun
    return func

This will lead to a error in python3 (at least in python 3.8 it does so), nonlocal n should be added to first line of fun.

Reference: Python nonlocal statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant