A program that plays rock-paper-scissors using Python

In this article A program that plays rock-paper-scissors using Python I will write.

How to find

1, Randomly generate a bot's move. 2, If it is Aiko, repeat it again. 3, Display the result of rock-paper-scissors

** 1, Randomly generate a bot's move. ** ** First of all, we will randomly generate the hands of the bot.

There are various ways, but this time I will do it with random.randint (0, 2). It can generate integers in the range in rendezvous.

We will apply this integer to Goo Choki Par.

janken.py


import random

def janken(your_result):
    n = random.randint(0, 2)
    if n == 0:
        bot = "Goo"
    elif n == 1:
        bot = "Choki"
    else:
        bot = "Par"

** 2, If it was Aiko, repeat again. ** ** Next, when I was Aiko, I will write to start over from the beginning.

I thought I could call the function again and start over.

janken.py


    if your_result == bot:
        j = input("Aiko, right?! > ")
        janken(j)

** 3, display the result of rock-paper-scissors ** Finally, the result of rock-paper-scissors is displayed. Ai In this case, it is excluded, so I will write a winning or losing pattern.

janken.py


    if your_result == "Goo":
        if bot == "Par":
            print("I got a par. I'm losing you.")
        elif bot == "Choki":
            print("I gave a choki. You win.")
    elif your_result == "Par":
        if bot == "Choki":
            print("I gave a choki. I'm losing you.")
        elif bot == "Goo":
            print("I put out a goo. You win.")
    elif your_result == "Choki":
        if bot == "Goo":
            print("I put out a goo. I'm losing you.")
        elif bot == "Par":
            print("I got a par. You win.")

Finally, get the source and then call the function to finish. Overall picture

janken.py


import random

def janken(your_result):
    n = random.randint(0, 2)
    if n == 0:
        bot = "Goo"
    elif n == 1:
        bot = "Choki"
    else:
        bot = "Par"
    
    if your_result == bot:
        j = input("Aiko, right?! > ")
        janken(j)

    if your_result == "Goo":
        if bot == "Par":
            print("I got a par. I'm losing you.")
        elif bot == "Choki":
            print("I gave a choki. You win.")
    elif your_result == "Par":
        if bot == "Choki":
            print("I gave a choki. I'm losing you.")
        elif bot == "Goo":
            print("I put out a goo. You win.")
    elif your_result == "Choki":
        if bot == "Goo":
            print("I put out a goo. I'm losing you.")
        elif bot == "Par":
            print("I got a par. You win.")

j = input("I'll play rock-paper-scissors!Enter goo, choki or par.> ")
janken(j)

Execution result

I will decide the first and second attack with rock-paper-scissors!Enter goo, choki or par.>Goo
Aiko, right?! >Goo
I got a par. I'm losing you.

Summary

This time, A program that plays rock-paper-scissors using Python I wrote.

There may be some grammar that isn't very good, but it works.

I think it's a relatively simple program, so please try to improve it yourself!

Thank you very much!

Note

This article was written by a programming beginner and may be incorrect. Thank you for your understanding. Also, if you notice any mistakes, we would appreciate it if you could point them out. Thank you.

Recommended Posts

A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
[Ev3dev] Create a program that captures the LCD (screen) using python
A program that removes duplicate statements in Python
[Python] A program that counts the number of valleys
[Python] A program that finds the minimum and maximum values without using methods
Using a Python program with fluentd's exec_filter Output Plugin
[Python] A program that compares the positions of kangaroos.
A Python program that converts ical data into text
Using a python program with fluentd's exec Output Plugin
There is a pattern that the program did not stop when using Python threading
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
I tried "a program that removes duplicate statements in Python"
[Python] A program that creates a two-dimensional array by combining integers
[Python] A program that finds the most common bird types
A Python program that aggregates time usage from icalendar data
Create a python GUI using tkinter
Drawing a silverstone curve using python
When writing a program in Python
A program that determines whether a number entered in Python is a prime number
[Python algorithm] A program that outputs Sudoku answers from a depth-first search
[Python] A program that rotates the contents of the list to the left
[Python] I tried to make a simple program that works on the command line using argparse.
I made a payroll program in Python!
[Python] Create a Batch environment using AWS-CDK
Write a Caesar cipher program in Python
Scraping a website using JavaScript in Python
[Python] Scraping a table using Beautiful Soup
[Python] Create a LineBot that runs regularly
Draw a tree in Python 3 using graphviz
A typed world that begins with Python
A Python script that automatically collects typical images using bing image search
[Python] A program that finds a pair that can be divided by a specified value
[Python] Created a Twitter bot that generates friend-like tweets using Markov chains
[Python] A program that calculates the number of socks to be paired
Try using APSW, a Python library that SQLite can get serious about
A python program that resizes a video and turns it into an image
[Python] Shopping program
A memo that I wrote a quicksort in Python
[Python] Split a large Flask file using Blueprint
[Python / Tkinter] A class that creates a scrollable Frame
A nice nimporter that connects nim and python
Nogizaka46 A program that automatically saves blog images
Create a page that loads infinitely with python
Start using Python
Publish a Python module that calculates meteorological factors
[Beginner] What happens if I write a program that runs in php in Python?
A program that searches for the same image
Publishing and using a program that automatically collects facial images of specified people
Create a web map using Python and GDAL
Make a rock-paper-scissors game in one line (python)
A simple Pub / Sub program note in Python
I tried reading a CSV file using Python
"Python Kit" that calls a Python script from Swift
I want to exe and distribute a program that resizes images Python3 + pyinstaller
Python: Create a class that supports unpacked assignment
PGM (python version) that plays videos while moving
[Python] A program that calculates the difference between the total numbers on the diagonal line.
Let's write a Python program and run it