pythonファイルに直接書き込まれた単語ではなく、テキストファイルの単語を使用するPythonでジャンブルゲームを作りたいとします。例えば、Pythonの読み込みテキストファイルを使用して文字列でリストを作成する
[['amazement', ' awe', ' bombshell', ' curiosity', ' incredulity', '\r\n'], ['godsend', ' marvel', ' portent', ' prodigy', ' revelation', '\r\n'], ['stupefaction', ' unforeseen', ' wonder', ' shock', ' rarity', '\r\n'], ['miracle', ' abruptness', ' astonishment\r\n']]
私は単語が1つのリストにソートしたい:私はそれらをインポートしたい場合でも、私はこのリストを取得
:["amazement", "awe", "bombshell"...]
は、これは私のpythonのコードです
amazement, awe, bombshell, curiosity, incredulity,
godsend, marvel, portent, prodigy, revelation,
stupefaction, unforeseen, wonder, shock, rarity,
miracle, abruptness, astonishment
:
import random
#Welcome the player
print("""
Welcome to Word Jumble.
Unscramble the letters to make a word.
""")
filename = "words/amazement_words.txt"
lst = []
with open(filename) as afile:
for i in afile:
i=i.split(",")
lst.append(i)
print(lst)
word = random.choice(lst)
theWord = word
jumble = ""
while(len(word)>0):
position = random.randrange(len(word))
jumble+=word[position]
word=word[:position]+word[position+1:]
print("The jumble word is: {}".format(jumble))
#Getting player's guess
guess = input("Enter your guess: ")
#congratulate the player
if(guess==theWord):
print("Congratulations! You guessed it")
else:
print ("Sorry, wrong guess.")
input("Thanks for playing. Press the enter key to exit.")
は、私は言葉でテキストファイルを持っています
ご協力いただきありがとうございます。
ワンライナーは、トリックをやりました!詳細な答えと説明をありがとう。 –