Make a rock-paper-scissors game in one line (python)

python3.8 + only

code

print("a|b|c"*-~-((a:=input("a:Par\nb:Choki\nc:Goo\n"))in"abc")or"Draw"*((b:=__import__("random").choice("abc"))==a)or"You "+"lwoisne"[b+a in"abbcca"::2],f"\nyou:{a} pc:{b}")

185 bytes

Commentary

First of all, maybe it can be shorter ...

Well to make it easier to understand

import random

a = input("a:Par\nb:Choki\nc:Goo")
b = random.choice("abc")
if -~-(a in "abc"):
    print("a|b|c")
elif a == b:
    print("Draw")
else:
    print("win" if b+a in "abbcca" else "lose")
print(f"you:{a} pc:{b}")

Is it like this? I change it a little.

Probably the most mysterious thing is -~-. This means that ~-is -1 (-~ is +) and it is further-. If ʻa in "abc" `is True (because bool is a subclass of int), it is -1 and becomes-, so it becomes -0, that is, 0, and if False, it becomes 1. In short, it means that not 〇〇 is written short. (In the case of the bottom, it becomes longer, but in the case of the top, it becomes shorter)

Then, is it b + a in" abbcca "? This is a judgment of winning or losing. It may be easier to understand if you separate it from ab / bc / ca.

__import__ is a function that imports the module specified by the argument and returns the object. (Isn't it correct?) Well, see here for details.

Is it something like ~~~ * ~~~ or ~~~ * ~~~ or ...? This uses the fact that the empty string becomes False in python. I usually feel like using it like str * bool.

Finally

Feel free to comment if you have any questions, if you can write shorter, or if you can write more clearly: smile:

Recommended Posts

Make a rock-paper-scissors game in one line (python)
Make python segfault in one line
Fizzbuzz in Python (in one line)
Make a bookmarklet in Python
Let's make a rock-paper-scissors game
CGI server (1) python edition in one line
Let's make a shiritori game with Python
Decompose command arguments in one line in Python
[Python] Invert bool value in one line
Let's make a combination calculation in Python
Make a squash game
One liner in Python
[Python] Make a game with Pyxel-Use an editor-
I tried playing a typing game in Python
I want to make a game with Python
Make a copy of the list in Python
[Python] Make a simple maze game with Pyxel
Make a joyplot-like plot of R in python
A note I looked up to make a command line tool in Python
Make a Tetris-style game!
Make one repeating string with a Python regular expression.
Try to make a Python module in C language
Specify a subcommand as a command line argument in Python
[LINE Messaging API] Create a rich menu in Python
Make a simple Slackbot with interactive button in python
Let's make a simple game with Python 3 and iPhone
[For play] Let's make Yubaba a LINE Bot (Python)
Make Python segfault on one line without using ctypes
[Super easy] Let's make a LINE BOT with Python.
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
DMD in Python one dimension
Try LINE Notify in Python
Don't make test.py in Python!
Make a LINE BOT (chat)
Make a fortune with Python
Make Opencv available in Python
Make python segfault in 2 lines
Draw a heart in Python
I want to write a triple loop and conditional branch in one line in python
I made a simple typing game with tkinter in Python
[In one line] Visualize like a lawn with just Pandas
Make a table of multiplication of each element in a spreadsheet (Python)
Decrypt one line of code in Python lambda, map, list
I made a puzzle game (like) with Tkinter in Python
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Let's make a number guessing game in your own language!
I tried to make a stopwatch using tkinter in python
I want to make input () a nice complement in python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[Python] Make your own LINE bot
[python] Manage functions in a list
Hit a command in Python (Windows)
Let's make a GUI with python.
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Make standard output non-blocking in Python
Implementation of life game in Python