[英]How to fix AttributeError: 'module' object has no attribute 'function'?
[英]How to fix AttributeError: module 'testing2' has no attribute 'printPackage'?
我有第一个模块:
#testing1
import testing2
choice = input('Enter your choice:')
def calculateMenuPrice(choice):
testing2.printPackage(menuList)
calculateMenuPrice(choice)
第二个模块:
#testing2
import testing1
menuList = testing1.calculateMenuPrice(choice)
def printPackage(menuList):
for x in menuList:
if menuList == '1':
return('''
----------
Menu List
----------
1. Jelly Fish Yee Sang with Pear
2. Dried Seafood with Fish Soup
3. Steamed Sea Water Grouper
''')
elif menuList == '2':
return('''
----------
Menu List
----------
1. Jelly Fish Yee Sang with Pear
2. Shark Fin Soup with Crab Meat
3. Steamed River Patin Fish
''')
elif menuList == '3':
return('''
----------
Menu List
----------
1. Salmon Fish Yee Sang with Pear
2. Steamed Classic Abalone Soup
3. Steamed Bamboo Fish
''')
elif menuList == '4':
return('''
----------
Menu List
----------
1. Abalone Yee Sang with Pear
2. Mini Classic Steam Soup
3. Steamed Local Pomfret Fish
''')
testing2模块要求我使用for循环遍历菜单,没有硬编码的代码,但是我得到了:
AttributeError: module 'testing2' has no attribute 'printPackage'
请帮助和建议。 我刚刚开始学习python。 谢谢。
从我所看到的,您得到错误的原因是因为两个模块都依赖于另一个。
如果看一下testing1
第1行,则会看到一个import语句。 然后运行testing2
。 该模块的第一行是导入testing1
。 当Python运行此模块,它找到一个参考testing2.printPackage
, 尚未进口的呢。
要解决此问题,请尝试整理两个模块的依赖关系,然后查看是否可以将两个模块组合到一个模块中。
另一种选择是,在testing2
的import语句testing2
,是这样定义printPackage
函数:
printPackage = lambda menuList: None
然后继续您的模块,稍后重新定义printPackage
函数。
希望这会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.