It's been a while since I posted an article. I was studying Python little by little while I wasn't publishing the article, so I feel like I've gained a sense of Python and programming.
So, I decided to make a shiritori and implemented it. In making shiritori -Take over the last letter of the previous word. = "Shiritori" ・ You cannot use words that you have already used. ・ If you end with "n", you lose. I took care to keep these three rules.
word_chain.py
hiragana_1 = "Shiritori"
used_hiragana = []
print("Shiritori start!\n", hiragana_1)
while True:
hiragana_2 = str(input("Please put hiragana: "))
if hiragana_2[0] != hiragana_1[-1]:
print("The first letter is incorrect.")
break
elif hiragana_2 in used_hiragana:
print("This word is already in use. You lose.")
break
elif hiragana_2[-1] == "Hmm":
print("This is a word that ends with "n". You lose.")
break
else:
used_hiragana.append(hiragana_1)
used_hiragana.append(hiragana_2)
hiragana_1 = hiragana_2
print("It's the next turn.")
(Execution result 1)
----Shiritori start!
Shiritori
Please put hiragana:Apple
It's the next turn.
Please put hiragana:Gorilla
It's the next turn.
Please put hiragana:Swallow
The first letter is incorrect.
(Execution result 2)
----Shiritori start!
Shiritori
Please put hiragana:Risu
It's the next turn.
Please put hiragana:Grated
It's the next turn.
Please put hiragana:Risu
This word is already in use. You lose.
I omitted the execution result a little, but I was able to make a shiritori according to the three rules I mentioned at the beginning. However, this code does not contain katakana, and the drawback is that it causes trouble when using words that include lowercase letters in hiragana. I want to overcome that and improve the accuracy, but it still seems a little difficult (laughs).
Recommended Posts