我有一个python脚本,该脚本在每个prime的文本文件中打印。我希望我的脚本可以将其关闭的列表获取,因此基本上将最后一行的内容作为变量。
这是我当前的脚本:
def calc():
while True:
x = 245747
y = (100**100)**100
for n in range (x,y):
if all(n%i!=0 for i in range (2,n)):
a=[]
a.append(n)
fo = open('primes.txt', 'a+')
print(n)
print ("", file = fo)
print ((a), file = fo)
fo.close
s = input('To do another calculation input yes, to quit input anything else...')
if s == 'yes':
continue
else:
break
calc()
我希望变量x作为输入的最后一行。如果最大的素数为245747,则应该在最后一行" [245747]"上。
我该如何实现?谢谢!
您可以使用读取线并获取列表的最后一项:
file = open("primes.txt", "r").readlines()
x = file[len(file)-1]
我认为这应该有效。您只能通过分裂或类似的东西摆脱2" [和"。