Python 导入自己的模块 - 名称未定义 [英] Python import own module - name not defined

查看:94
本文介绍了Python 导入自己的模块 - 名称未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新我的基本混乱游戏.我已经做到了脚本从文本文件中获取单词,现在我想将它们分成模块,因为我有不同的文本文件和不同的单词.

I want to update my basic jumble game. I already did that the script gets words from a text file, now I want to separate them into modules as I have different text files with different words.

我有我的主脚本 jumble_game.py:

I have my main script, 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 导入 jumble_game.py,因为我希望用户选择组,从中选择单词.

I want the file amazement.py to be imported into jumble_game.py because I want the user to select the group, from which the words will be chosen.

惊奇.py:

filename = "amazement_words.txt"

我收到此错误:

File "jumble_game.py", line 49, in <module>
    wordlist(filename)
NameError: name 'filename' is not defined

如果我以另一种方式执行,将主脚本导入 amazement.py 并运行后者,则代码运行没有问题.

If I do it the other way, importing the main script into amazement.py and run the latter, the code functions without a problem.

任何线索我错过了什么?仍然是 Python 初学者,所以请耐心等待.:)

Any clue what am I missing? Still a Python beginner so bear with me. :)

感谢您的帮助/建议!

推荐答案

您所陈述的问题是标准命名空间/范围问题.您已经在 amazement.py 的范围内创建了一个变量,但不在 jumble_game.py 命名空间中.因此,如果不告诉程序从何处获取该变量,就无法访​​问 amazement.py 中的顶级变量.

The problem you've stated is a standard namespace/scope problem. You've created a variable within the scope of amazement.py, but not in the jumble_game.py namespace. As a result, you can't access the top level variable in amazement.py without telling your program from where to get that variable.

你可以做一些事情.我将列出两个:

You can do a few things. I'll list two:

1.

来自惊奇导入文件名

这将允许您使用您所描述的术语文件名".

This will allow you to use the term "filename" as you've described.

或 2.

将任何对 filename 的引用替换为 amazement.filename.

Replace any reference to filename with amazement.filename.

您可以在此处阅读有关范围和命名空间的更多信息:http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html

You can read more about scopes and namespace here: http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html

这篇关于Python 导入自己的模块 - 名称未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆