Python AttributeError:模块' xxx'没有属性' xxx'

发布于 2025-02-09 00:51:37 字数 1172 浏览 1 评论 0原文

我是Python的新手,我正在从事一个个人项目,以解决更舒适的问题解决和计划。我正在尝试在另一个模块中引用一个变量,但我似乎无法使它起作用,这使我发疯。我已经四处寻找类似的话题,但恐怕我对这些东西还不够了解,无法阅读解决方案,并弄清楚如何将它们应用于我的项目。这是来自“ stapeOne.py”的代码:

import difficultySelection
difficultySelection.diffSel()
print(f'   {difficultySelection.difficulty1}   ')

这是“难以选择”的代码:

def diffSel(mode_selected = False):
     while mode_selected == False:
          print('Select difficulty (normal/hard)')
          mode_input = input()
          if mode_input == "normal":
               mode_selected = True
               difficulty1, difficulty2, difficulty3, difficulty4 = 1,2,3,4
               return mode_selected, difficulty1, difficulty2, difficulty3, difficulty4
          elif mode_input == "hard":
               mode_selected = True
               difficulty1, difficulty2, difficulty3, difficulty4 = 2,4,6,8
               return mode_selected, difficulty1, difficulty2, difficulty3, difficulty4
          else:
               print("Input not accepted, please enter 'normal' or 'hard'")

使用此设置,我不断收到错误“ attributeError:module'refflasselection'没有属性'lublicyy1'where cluckySelection.difficulty1“称为”。 。

I'm new to Python and I'm working on a personal project to get more comfortable problem solving and planning things out. I'm trying to reference a variable in another module but I can't seem to get it working and it's driving me crazy. I've done some looking around at similar topics but I'm afraid I don't know enough about this stuff yet to read into the solutions and figure out how to apply them to my project. Here is the code from "phaseOne.py":

import difficultySelection
difficultySelection.diffSel()
print(f'   {difficultySelection.difficulty1}   ')

Here is the code from "difficultySelection":

def diffSel(mode_selected = False):
     while mode_selected == False:
          print('Select difficulty (normal/hard)')
          mode_input = input()
          if mode_input == "normal":
               mode_selected = True
               difficulty1, difficulty2, difficulty3, difficulty4 = 1,2,3,4
               return mode_selected, difficulty1, difficulty2, difficulty3, difficulty4
          elif mode_input == "hard":
               mode_selected = True
               difficulty1, difficulty2, difficulty3, difficulty4 = 2,4,6,8
               return mode_selected, difficulty1, difficulty2, difficulty3, difficulty4
          else:
               print("Input not accepted, please enter 'normal' or 'hard'")

With this setup I keep receiving the error "AttributeError: module 'difficultySelection' has no attribute 'difficulty1'" where "difficultySelection.difficulty1" is called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2025-02-16 00:51:37

当您执行这样的返回语句时,您需要函数调用器才能捕获返回的数据。你没有这样做。要捕获返回的数据(这里是元组),您需要对此进行修改:

mode_selected, diffculty1, difficulty2, difficulty3, difficulty4, = difficultySelection.diffSel()
print(f'   {difficulty1}   ')

When you do a return statement like that, you need the function caller to capture the returned data. You are not doing that. To capture the returned data (which is a tuple here), you need to modify your call to this:

mode_selected, diffculty1, difficulty2, difficulty3, difficulty4, = difficultySelection.diffSel()
print(f'   {difficulty1}   ')
月野兔 2025-02-16 00:51:37

弄清楚了!原来,我没有正确地引用另一个模块中的变量。我将参考文献从``refferySelecle.difficulty1''转换为“ rugidSeleSelection.diffsel()。难度1”,这似乎可以解决问题。可能会造成新的问题,但这只是解决的另一个挑战!

Figured it out! Turns out I wasn't referencing the variable in the other module properly. I switched my references from "difficultySelection.difficulty1" to "difficultySelection.diffSel().difficulty1" and that seemed to fix things. Might create new problems but that's just another challenge to tackle!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文