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.
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 ().
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.
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).
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.
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