In this article A program that plays rock-paper-scissors using Python I will write.
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.
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!
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