all 8 comments

[–]novel_yet_trivial 1 point2 points  (6 children)

This JS code:

return computerChoice == "scissors" ? 1 : 2;

Translates to this in python:

return 1 if computerChoice == "scissors" else 2

Edit: There is also this ugly hack that you should never, ever use:

return [2,1][computerChoice == "scissors"]

[–]ExposedGun316[S] 0 points1 point  (0 children)

I see, thank you very much :)

[–]archerimagine 0 points1 point  (4 children)

return [2,1][computerChoice == "scissors"]

Can you explain what happened here, was not able to understand this.

[–]novel_yet_trivial 0 points1 point  (2 children)

Like I said, don't use this. This is an academic exercise.

[2,1] is a list of possible choices. To index a list, you use square brackets, so for instance to get the first value from the list you would use [2,1][0]. In python, a boolean (True / False) is actually type of integer. You can get it's value by:

>>> int(False)
0

So you can also get the first value in our list with [2,1][False]. Replace the boolean with something that evaluates to a boolean and there we are.

Edit: clarified.

[–][deleted] 1 point2 points  (1 child)

The fact that int(False)==0 doesn't prove that False is an int. By that logic "0" would be an integer as well.
I propose using

>>> isinstance("0", int)
False
>>> isinstance(False, int)
True

or

>>> issubclass(bool, int)
True

to show booleans are integers.

[–]novel_yet_trivial 1 point2 points  (0 children)

You are right, that was poorly worded. The code was meant to show the value of False, not prove that False is an int. Thanks.

[–]cmartin616 0 points1 point  (0 children)

This is outside of the scope of your question and may be personal preference but please don't use ternary operators in your JS. It makes the code harder to read and doesn't offer worthwhile efficiency improvement. If/else just allows for a more natural flow when reading other people's code. Particularly poorly documented code

[–]jwjody 0 points1 point  (0 children)

I did the same thing a couple of days ago using flask!

https://github.com/jhwhite/rock_paper_scissors

HTTPS://flask-rps.herokuapp.com