Python-100-Days/Day01-15/code/Day11/file3.py

27 lines
485 B
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
写文本文件
将100以内的素数写入到文件中
Version: 0.1
Author: 骆昊
Date: 2018-03-13
"""
from math import sqrt
def is_prime(n):
2019-05-03 21:17:36 +08:00
for factor in range(2, int(sqrt(n)) + 1):
if n % factor == 0:
return False
return True
2018-04-27 00:00:22 +08:00
# 试一试有什么不一样
# with open('prime.txt', 'a') as f:
with open('prime.txt', 'w') as f:
2019-05-03 21:17:36 +08:00
for num in range(2, 100):
if is_prime(num):
f.write(str(num) + '\n')
2018-04-27 00:00:22 +08:00
print('写入完成!')