[Episode 2] Beginners tried Numeron AI with python

0th 1st 2nd 3rd Final story

About this article

This time I will finally write a program I wrote the flow of the program in Part 1, but I do not necessarily write it in that flow, so thank you. The python ver is 3.8.2.

Determining player and com numbers

Here, we will create a program to determine your own number and com number. I'm thinking of playing by myself, so I'll write instructions with print ().

Com number determination

Then from com. I want to determine the numbers at random, so first import the random module. Then let's shuffle [0,1,2,3,4,5,6,7,8,9] to extract the first three characters. If you write specifically

import random
NUMBERS=[0,1,2,3,4,5,6,7,8,9]
random.shuffle(NUMBERS)
com=NUMBERS[0:3]

#print(com)
#[7,0,2]

It will be. Of course, this time it was [7,0,2], but this number changes every time. Next, I will write a program that determines the number of players.

Player number determination

The number of the player is decided, but I think it's okay if it's random. However, I think that some people have a favorite number, so let's make it possible to input ().

for i in range(10**9):
    print("Please enter your favorite 3-digit number with all numbers different")
    X=input()
    if len(X) != 3:
        print("Please use 3 digits")
    else:
        if int(X[0]) in NUMBERS and int(X[1]) in NUMBERS and int(X[2]) in NUMBERS:
            if X[0] == X[1] or X[0] == X[2] or X[2] == X[3]:
                print("Please enter all three numbers differently")
            else:
                player=[int(X[0]) , int(X[1]) , int(X[2])]
                break
        else:
            print("Please use a 3-digit "number"")

This time, I programmed it to input until it became a "3-digit number". Of course, if you fail 10 ^ 9 times, there is nothing you can do about it, but if you do that, you can't help it (appropriate).

Function to confirm EAT, BITE

Next, we will create a program that will check if the called number is xEAT, yBITE. Let's name the function NUMERON

def NUMERON(CALL,ANS):
    EAT=0
    BITE=0
    for i in range(3):
        if CALL[i]==ANS[i]:
            EAT+=1
        elif CALL[i] in ANS and CALL[i]!=ANS[i]:
            BITE+=1
    return [EAT,BITE]

If you explain the program +1 to EAT if there is a place and a number +1 to BITE if it is included but in a different location Will do it.

Up to here for this time

For the time being, it has gradually become more like Numeron. However, I only made a number input program and EAT and BITE functions.

To summarize this program into one

import random

def NUMERON(CALL,ANS):
    EAT=0
    BITE=0
    for i in range(3):
        if CALL[i]==ANS[i]:
            EAT+=1
        elif CALL[i] in ANS and CALL[i]!=ANS[i]:
            BITE+=1
    return [EAT,BITE]

NUMBERS=[0,1,2,3,4,5,6,7,8,9]
random.shuffle(NUMBERS)
com=NUMBERS[0:3]

for i in range(10**9):
    print("Please enter your favorite 3-digit number with all numbers different")
    X=input()
    if len(X) != 3:
        print("Please use 3 digits")
    else:
        if int(X[0]) in NUMBERS and int(X[1]) in NUMBERS and int(X[2]) in NUMBERS:
            if X[0] == X[1] or X[0] == X[1] or X[0] == X[1]:
                print("Please enter all three numbers differently")
            else:
                player=[int(X[0]) , int(X[1]) , int(X[2])]
                break
        else:
            print("Please use a 3-digit "number"")

is. It will be fun if it gets longer gradually! (Is it just me?) Next time, we will finally make AI!

Recommended Posts

[Episode 2] Beginners tried Numeron AI with python
[Episode 3] Beginners tried Numeron AI with python
[Episode 0] Beginners tried Numeron AI with python
[Episode 1] Beginners tried Numeron AI with python
[Final story] Beginners tried Numeron AI with python
3. 3. AI programming with Python
I tried fp-growth with python
I tried scraping with Python
Make Puyo Puyo AI with Python
Mayungo's Python Learning Episode 1: I tried printing with print
I tried gRPC with Python
I tried scraping with python
[Ipdb] Web development beginners tried to summarize debugging with Python
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried web scraping with python.
I tried running prolog with python 3.8.2.
python beginners tried to find out
I tried SMTP communication with Python
I tried to solve the ant book beginner's edition with python
[Pandas] I tried to analyze sales data with Python [For beginners]
Mayungo's Python Learning Episode 7: I tried printing with if, elif, else
INSERT into MySQL with Python [For beginners]
I tried scraping Yahoo News with Python
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
[Python] Read images with OpenCV (for beginners)
Mayungo's Python Learning Episode 8: I tried input
WebApi creation with Python (CRUD creation) For beginners
How Python beginners get started with Python with Progete
Build AI / machine learning environment with Python
[For beginners] Try web scraping with Python
#I tried something like Vlookup with Python # 2
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
I refactored "I tried to make Othello AI when programming beginners studied python"
I tried to predict next year with AI
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Causal reasoning and causal search with Python (for beginners)
I tried "differentiating" the image with Python + OpenCV
Scraping with Python
Python with Go
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Python beginners tried to code some energy drinks
Twilio with Python
I tried L-Chika with Raspberry Pi 4 (Python edition)
Integrate with Python
Play with 2016-Python
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
AES256 with python
~ Tips for Python beginners from Pythonista with love ① ~
I tried function synthesis and curry with python
Tested with Python
I tried to output LLVM IR with Python
Machine learning beginners tried to make a horse racing prediction model with python