Ad
  • Custom User Avatar

    "0=0" is an edge case added on request.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Yes, anything defined outside a function is in a "global scope", and anything defined within the function is (normally) in a "local scope". Scopes are common to most programming languages, but how they function exactly can vary. (For instance, Python tends to just have local and global scopes, but in e.g. C, C++, or Java, a function can have many scopes, and a variable might only exist within the body of a loop or whatever.)

    Yes, a dictionary is basically a list, but instead of each element having a "number" index, you can use anything as the index. In other languages, these are sometimes called "maps" (Java, C++) or "hashes" (Perl, Ruby). I've also heard the term "associative array".

    Yes, a "lookup" expression like foo[k] produces a value, and you can perform more lookups on that result using foo[k][k2]. You often see this with attributes and methods -- foo.bar.baz or foo.bar().baz(). For example, you might have done request.POST.iteritems(). From the point of view of "abstract syntax", a "lookup expression" is E1[E2] where both E1 and E2 can be expressions. The concept of expressions is deeply recursive so you can build up expressions of arbitrary complexity like foo[k1][bar.baz[k2]]. This is IMO one of the most beautiful ideas in computer science, but expressions above a certain threshhold of complexity should probably get broken out into separate variables. I think return dict[choice][outcome] is fine and good, but if you felt like it wuold aid readability, you might do

    # outcome = 0 for loss, 1 for win, or whatever
    responses = dict[choice]
    return responses[outcome]
    

    Re: the return foo if ..... solution. In other programming languages, there is a special if/then/else expression. In the C family of languages, it's written condition ? trueval : falseval. So console.log(win ? "you won!" : "you lost"); . For a long time, Python didn't have this kind of expression, but eventually it was added with a (IMO) very quirky syntax: trueval if condition else falseval, so print("you won!" if win else "you lost") . The Python community thinks that this emphasizes the "default" case where the condition is true. Anyhow, so you can see that the "if/else" thing here is an expression, not a statement, and you can return any expression. (You can't do e.g. return while .....) I'm not actually sure how his solution works -- it seems like it will sometimes produce scissors-scissors ties, which I thought was forbidden?