- 帖子
- 1
- 精华
- 0
- 积分
- 16
- 阅读权限
- 10
- 注册时间
- 2016-5-21
- 最后登录
- 2016-7-14
|
# coding: utf-8
# Filename: randint.py
# 从1~n中,随机取m个数, 1 <= m <= n
from random import randint
limits = int(raw_input("随机数的上限是:"))
times = int(raw_input("抽取的次数:"))
class Guessing_game(object):
def __init__(self, guessing_times, limit_num, lists):
self.guessing_times = guessing_times
self.limit_num = limit_num
self.lists = lists
def play(self):
cnt = 1
result = 'result = {\n'
while self.guessing_times > 0:
num = randint(0, self.limit_num)
if num in self.lists: # 删除重复的随机数
num = randint(0, self.limit_num)
else:
if self.guessing_times != 1:
line = " '第%r次抽取的数是': '%r',\n" % (cnt, num)
result += line
cnt += 1
else:
line = " '第%r次抽取的数是': '%r'\n" % (cnt, num)
result += line
cnt += 1
self.lists.append(num)
self.guessing_times -= 1
self.lists = []
cnt = 0
result += "\n}"
return result
def file_saving(self, data):
f = file("e:\Coding\Python\keng1\data.py", 'w')
f.write(data)
f.close()
game = Guessing_game(times, limits, [])
game.file_saving(game.play())
|
|