创建文件data.txt,文件共100行,每行存放一个1~100之间的整数,最后读取文件里面的值
import random
f = open('data.txt', 'a+')
for i in range(100):
f.write(str(random.randint(1, 100)) + '\n')
f.seek(0, 0)
print(f.read())
f.close()
往hello.txt的文本文件中写入两行文本 hello,world\nhello,python (使用utf-8编码)
f = open('hello.txt', 'w+', encoding='utf-8')
f.write("hello,world\nhello,python")
f.close()
求1+2!+3!+…10!的阶乘,利用循环完成
for i in range(1,11):
sum1=1
j=1
while j<=i:
sum1=sum1*j
j+=1
sum+=sum1
print(f'1到10的阶乘之和为{sum}')