|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下
- # -*- coding: utf-8 -*-
- """
- Created on Sat Nov 28 20:59:39 2020
- @author: Administrator
- """
- import random
- jihe=[]
- def bg():
- jihe=[]
- f=open('F://aaa.txt','r',encoding='utf-8')
- a=f.readlines()
- a=list(a)
- f.close()
- while 1:
- s=random.randint(0,len(a))
- if s in jihe:
- continue
- else:
- jihe.append(s)
- if len(jihe)==10:
- break
- print(jihe)
- asd=str(a[jihe[0]]+a[jihe[1]]+a[jihe[2]]+a[jihe[3]]+a[jihe[4]]+a[jihe[5]]+a[jihe[6]]+a[jihe[7]]+a[jihe[8]]+a[jihe[9]])
- print(asd)
- while 1:
- if input('')=='1':
- bg()
复制代码
文件夹有26行
运行两次没问题
三次报错
请问如何解决?
本帖最后由 jackz007 于 2020-11-28 23:46 编辑
楼主可以试试这个代码
- # -*- coding: utf-8 -*-
- """
- Created on Sat Nov 28 20:59:39 2020
- @author: Administrator
- """
- import random
- def bg():
- jihe = []
- f=open('F://aaa.txt','r',encoding='utf-8')
- a = list(f)
- f . close()
- k = 0
- while k < 10:
- s = random . randint(0 , len(a) - 1) # 楼主的代码问题出在此处
- if not s in jihe:
- jihe.append(s)
- k += 1
- print(jihe)
- asd = "" . join(a[x] . strip() for x in jihe)
- print(asd)
- while 1:
- if input('')=='1':
- bg()
复制代码
|
|