发布网友 发布时间:2022-04-23 06:45
共4个回答
热心网友 时间:2022-06-16 22:26
第一道题,可能写的有点麻烦,可以简化。: /
import random
#获取随机数并写入
for i in range(10):
a = str(random.randrange(1,100))
file = open("file.txt","a+")
file.write(a)
file.write(" ")
#写入结束
#读取
file = open("file.txt",'r')
number = file.read()
nb = number.split()
a = 0
list = []
for i in range(10):
number = int(nb[a])
a += 1
list.insert(0,number)
list.sort()
b = 0
for i in range(10):
print(list[b],end = " ")
b += 1
file.close()
我把他转成列表排序再输出了。
热心网友 时间:2022-06-16 22:26
100解决问题
热心网友 时间:2022-06-16 22:27
第一题:
import random
num = range(1,100)
nums = random.sample(num,10)
f= open('E:\其他\python\data\随机数.txt',"w")
for i in range(10):
f.write(str(nums[i]))
if i == 9:
break
f.write(" ")
f.close()
with open('E:\其他\python\data\随机数.txt',"r") as df:
data = df.readline()
datas = data.split(' ')
#print(datas)
for i in range(10):
for j in range(i,10):
if datas[j] < datas[i]:
temp = datas[j]
datas[j] = datas[i]
datas[i] = temp
print(datas)
第二题:
with open('E:\其他\python\data\英文短文.txt',"r") as df:
data = df.readlines()
L = len(data)
print("该文章行数为:",L) #行数
str_total = 0
for i in range(L):
str_total += len(data[i])
print("该文章字符数为:",str_total)
words_total = 0
for i in range(L):
data[i].replace(',',' ')
data[i].replace('.',' ')
data[i].replace('?',' ')
data[i].replace('!',' ')
words_total += len(data[i].split(' '))
print("该文章单词数为:",words_total)
热心网友 时间:2022-06-16 22:27
很简单 可以帮写