【小鼓捣】带历史的伪随机数抽签
为了某些事情,临时检索缝合了这么个简陋程序,就当是为以后正式系统学习python做个引子吧
功能命令:
y / n : 是否抽签
show history : 查看历史
clear memory : 清除历史
from collections import deque
import pickle
import random
def main():
hq=deque([],50)
hq=pickle.load(open('save2.pkl','rb'))
while True:
line = input('是否进行下一次抽签(y/n):')
if line == 'y' :
n=random.randint(1,30)
print('中签:%d' % n)
hq.append(n)
pickle.dump(hq,open('save2.pkl','wb'))
hq2=pickle.load(open('save2.pkl','rb'))
print('<History>:')
print(list(hq2))
elif line == 'n' :
break
elif line == 'show history':
hq2=pickle.load(open('save2.pkl','rb'))
print('<History>:')
print(list(hq2))
elif line == 'clear memory':
hq.clear()
pickle.dump(hq,open('save2.pkl','wb'))
if __name__ == '__main__':
main()
涉及
1.import 和 from...import
2.random模块
3.pickle模块和pkl文件
4.collection模块中的deque
5.基本输入输出,list,函数调用