从文件创建 table
Creating a table from a file
我必须编写一个程序来创建部分幻方 table。
当 运行 我的程序 table 打印如下:
[int, int, int, int] [int, int, int, int] [int, int, int, int].
我想让它打印出我的 table:
[int, int, int, int]
[int, int, int, int]
[int, int, int, int]
我的问题是,当它从文本文件中读取时我该如何处理?
我的以下代码尝试是这样的:
text_file = input("Enter the name of the text file: ")
text = open(text_file, "r")
N = int(input("Enter the magic number: "))
print("This is the table")
table = []
nested_list = []
table = text.readlines()
i = 0
for line in table:
line = line.rstrip()
line = line.split(" ")
nested_list = []
for number in line:
nested_list.append(int(number))
table[i] = nested_list
i += 1
for index in range(1):
print(table)
不确定我是否理解问题,但这行不通吗?
for t in table:
print(t)
我必须编写一个程序来创建部分幻方 table。 当 运行 我的程序 table 打印如下:
[int, int, int, int] [int, int, int, int] [int, int, int, int].
我想让它打印出我的 table:
[int, int, int, int]
[int, int, int, int]
[int, int, int, int]
我的问题是,当它从文本文件中读取时我该如何处理? 我的以下代码尝试是这样的:
text_file = input("Enter the name of the text file: ")
text = open(text_file, "r")
N = int(input("Enter the magic number: "))
print("This is the table")
table = []
nested_list = []
table = text.readlines()
i = 0
for line in table:
line = line.rstrip()
line = line.split(" ")
nested_list = []
for number in line:
nested_list.append(int(number))
table[i] = nested_list
i += 1
for index in range(1):
print(table)
不确定我是否理解问题,但这行不通吗?
for t in table:
print(t)