![見出し画像](https://assets.st-note.com/production/uploads/images/10490480/rectangle_large_type_2_4325e88156cb8deeba0cd6ea0e6ec9fa.jpeg?width=1200)
Photo by
tomekantyou1
[Python]27行で作る単語並び替えゲーム
1.単語並び替えゲーム
ランダムに並び替られた単語を元の単語に並び替えて答えるゲームです。不正解の時は元の単語の一部を*印で隠してヒントを出すようにします。
2.コード
from random import sample
from random import randint
#問題リスト
a = {"Java": "", "Ruby": "", "PHP":"", "Python":""}
#入れ替え処理
for word in a.keys():
#一文字づつ区切り
splitted_word = list(word)
#ランダムに要素数分抽出する
random_word = sample(splitted_word, len(splitted_word))
#カンマ区切りで連結
cat_comma = ",".join(random_word)
a[word] = cat_comma
#ゲームスタート
for i, data in enumerate(a.items()):
word, question = data
print("Q{0}.{1}".format(i + 1, question))
answer = input("元の言葉は何だっでしょうか?:")
if answer == word:
print("Correct!=>{0}".format(word))
else:
hint = list(word)
secret_index = randint(0, len(hint))
hint[secret_index - 1] = "*"
hint = "".join(hint)
print("Booo!=>[Hint:{0}]".format(hint))
3.実行結果
Q1.J,a,a,v
元の言葉は何だっでしょうか?:Java
Correct!=>Java
Q2.u,y,R,b
元の言葉は何だっでしょうか?:Ruby
Correct!=>Ruby
Q3.P,H,P
元の言葉は何だっでしょうか?:HPP
Booo!=>[Hint:P*P]
Q4.y,t,h,n,o,P
元の言葉は何だっでしょうか?:Python
Correct!=>Python