我有第一个模块,它需要打开文件,其中包含娱乐列表:
#testing1
def printEntertainment (venueChoice,entertainmentList,venueList):
if venueChoice == '1':
if entertainmentList == '1':
return('There are no entertainment for VIP Room')
elif entertainmentList == '2':
return('There are no entertainment for Executive Room')
elif entertainmentList == '3':
return('''
Entertainment for Pool site''')
x = open('addon/entertainment.txt').readlines()
desired_lines = [1,2,3]
output = []
for desired in desired_lines:
output.append(x[desired - 1])
for o in output:
return("{}.{}".format(output.index(o) + 1, o))
elif entertainmentList == '4':
return('''
Entertainment for Banquet Hall''')
x = open('addon/entertainment.txt').readlines()
desired_lines = [2,3,4,5]
output = []
for desired in desired_lines:
output.append(x[desired - 1])
for o in output:
return("{}.{}".format(output.index(o) + 1, o))
第二个模块是选择娱乐场所和号码:
#testing2
import testing1
print('''
1.Vip room 2.Executive room
3.pool site 4. Banquet Hall
''')
venueChoice = input('Please select a venue:')
entertainmentList = input('enter no. for entertainment:')
venueList = None
testing2.printEntertainment (venueChoice,entertainmentList,venueList)
choice = testing2.entertainmentList
venueChoice = input('Please enter choice:')
def calculateEntertainment(choice,venueChoice):
if choice == '1':
print(0)
elif choice == '2':
print(0)
elif choice == '3':
if venueChoice == '1':
print(2000)
elif venueChoice == '2':
print(250)
elif venueChoice == '3':
print(400)
elif choice == '4':
if venueChoice == '1':
print(250)
elif venueChoice == '2':
print(400)
elif venueChoice == '3':
print(1000)
elif venueChoice == '4':
print(1500)
似乎我不能从printEntertainment (venueChoice,entertainmentList,venueList)
使用娱乐列表,我在choice = testing2.entertainmentList
使用它。它将打印出module 'testing1' has no attribute 'entertainmentList'
。
我应该在程序中添加什么来修复它?谢谢
我觉得你错了很多。您正在从不同的python文件('testing1.py')调用函数。因此,要调用函数,它应该是
testing1.printEntertainment()
,如果要将函数的输出保存为testing2
,可以这样做:testing2 = testing1.printEntertainment()
。似乎还需要处理类属性之类的变量:testing2.entertainmentList
正在尝试从类testing2
获取属性entertainmentList
,而您从未初始化该属性。所以我建议你再看看一些基本的教程。对于你的代码,你可以把两个文件放在一个文件中,跳过你目前正在做的很多事情。但我不知道你想达到什么目的,所以这里有一些基本的改进,可能会帮助你得到这个想法:相关问题 更多 >
编程相关推荐