私は基本的なジャンブルゲームを更新したいと思います。私はすでにスクリプトがテキストファイルから単語を取得したことを確認しましたが、別の単語を持つ異なるテキストファイルがあるので、それらをモジュールに分割したいと思います。Python import ownモジュール名が定義されていません
私は私のメインのスクリプト、jumble_game.py持っている:私は、ユーザがグループを選択するための言葉は意志そこから、私はファイルamazement.pyがjumble_game.pyにインポートしたい
import random
import amazement
#Welcome the player
print("""
Welcome to Word Jumble.
Unscramble the letters to make a word.
""")
def wordlist(file):
with open(file) as afile:
global the_list
the_list = [word.strip(",") for line in afile for word in line.split()]
print(the_list)
def main():
score = 0
for i in range(4):
word = random.choice(the_list)
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: ").lower()
#congratulate the player
if(guess==theWord):
print("Congratulations! You guessed it")
score +=1
else:
print ("Sorry, wrong guess.")
print("You got {} out of 10".format(score))
#filename = "words/amazement_words.txt"
wordlist(filename)
main()
を選択される。
amazement.py:
File "jumble_game.py", line 49, in <module>
wordlist(filename)
NameError: name 'filename' is not defined
私はそれを他の方法で、後者をamazement.pyにメインスクリプトをインポートし、実行を行う場合は、コード:
filename = "amazement_words.txt"
私はこのエラーを取得します問題なく機能します。
私は何が欠けていますか?まだPythonの初心者は私と一緒に耐える。 :)
ありがとうございました!
なぜ 'amazement.filename'を使わないのでしょうか? –
または 'from amazement import *'は、 'amazement'という接頭辞なしで' filename'にアクセスできるようにするべきです。 –
これは完璧です、ありがとうございます。しかし、後で私は別のテキストファイルからの単語でsad.pyを持つでしょう。 'from ... import *'を使う方が良いですか? –