我编写了一个Python工具来计算给定范围内的质数。然后我决定复制shell中的数字,创建一个txt文件,每次都粘贴它们是一个有点麻烦的事情,如果我能用这个工具将素数插入到文本文件中会非常方便。在
我试过了:
def calc():
while True:
x = int(input("Please specify the lower end of the range...."))
y = int(input("Please specify the upper end of the range...."))
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', 'w')
print (">>>Writing the values to primes.txt...")
print ("##########Calculated by my prime calculator##########", file = fo)
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()
编辑:
与open一起使用(“素数.txt因为我解决了这个问题
但是,我无法让Python将n个值保存到内存中并将它们附加到一个不断增长的列表中。在
你们真是太棒了。关于Python愚蠢的那部分是一个幽默的开始
也许你想
^{pr2}$实际上,您根本不应该这样做,您应该使用with来安全地打开文件,并且只在循环之外这样做一次
相关问题 更多 >
编程相关推荐