如何修复模块' ...'没有属性' ....'

时间:2017-11-19 06:58:47

标签: python python-3.x

我有第一个模块需要打开文件,里面有娱乐列表:

#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'。 我应该在程序中添加什么来修复它?感谢

1 个答案:

答案 0 :(得分:0)

我认为你犯了很多错误。你正在从一个不同的python文件('testing1.py')调用一个函数。因此,要调用该函数,它应该是testing1.printEntertainment(),如果要将函数的输出保存为testing2,则可以这样执行:testing2 = testing1.printEntertainment()。您似乎还想处理类属性等变量:​​testing2.entertainmentList正试图从类entertainmentList获取您从未初始化的属性testing2。所以我建议你再看看一些基本的教程。对于你的代码,你可以把这两个文件合二为一,跳过你目前正在做的很多东西。但我不知道你想要达到什么目标,所以这里只是一些基本的改进,可以帮助你理解这个想法:

# 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
print('''1.Vip room     2.Executive room
3.pool site    4. Banquet Hall''')
# You also should specify what input you want to have (e.g. string or int)
venueChoice = str(input('Please select a venue:'))
entertainmentList = str(input('enter no. for entertainment:'))
venueList = None
testing1 = printEntertainment(venueChoice, entertainmentList, venueList)


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)


calculateEntertainment(entertainmentList, venueChoice)