python中的主函数调用_调用主函数中的函数

我写了一个程序,想调用main中的函数。但是,我收到了一个语法错误。我不知道我做错了什么。这是我的代码,我尝试了一些东西,但是主函数不会调用其余的函数。在class Matrix(object):

def open_file():

'''Opens the file if it exists, otherwise prints out error message'''

Done = False

while not Done:

try:

File = input("Enter a filename: ").lower() #Asks user for a file to input

Open_File = open(File, "r") #Open the file if it exists and reads it

Info = Open_File.readlines()[1:]

Open_File.close() #Close the file

Done = True #Exits the while loop

except FileNotFoundError:

print("Sorry that file doesn't exist!") #prints out error message if file doesn't exist

return Info #Info is the file_pointer(fp)

def __init__(self): # This completed method is given

'''Create and initialize your class attributes.'''

self._matrix = {} #Intialize the matrix

self._rooms = 0 #Set the rooms equal to zero

def read_file(self, Info): #Info is equvalient to the file pointer or fp

'''Build an adjacency matrix that you read from a file fp.'''

self._rooms = Info.readline()

self._rooms = int(self._rooms)

for line in Info:

a, b = map(int, line.split())

self._matrix.setdefault(a, set()).add(b)

self._matrix.setdefault(b, set()).add(a)

return self._rooms and self._matrix

def __str__(self):

'''Return the adjacency matrix as a string.'''

s = str(self._matrix)

return s #__str__ always returns a string

def main(self):

matrix = Matrix()

info = matrix.open_file()

matrix.read_file(info)

s = str(matrix)

print(s)

if __name__ == '__main__':

m = Matrix()

m.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值