Python AttributeError:模块' xxx'没有属性' xxx'
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行这样的返回语句时,您需要函数调用器才能捕获返回的数据。你没有这样做。要捕获返回的数据(这里是元组),您需要对此进行修改:
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:
弄清楚了!原来,我没有正确地引用另一个模块中的变量。我将参考文献从``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!