码迷,mamicode.com
首页 > 其他好文 > 详细

生成计算题

时间:2018-10-16 19:58:28      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:dom   lse   bsp   生成   creat   xls   yield   random   col   

import random, os
import xlsxwriter

def create_dgit(num):
    count = 0
    foo = [lambda x, y: x+y, lambda x, y: x-y]
    while True:
        if count == num:
            break
        a, b, c = random.randint(1, 20), random.randint(1, 20), random.randint(0, 1)
        if foo[c](a, b) <= 20 and foo[c](a, b) > 0:
            yield "{} + {} =".format(a, b) if c == 0 else "{} - {} =".format(a, b)
            count += 1

if __name__ == "__main__":
    row, col = 0, 0
    workbook = xlsxwriter.Workbook(exercises.xlsx)
    worksheet = workbook.add_worksheet()

    for i in create_dgit(1000):
        if col > 4:
            row += 1
            col = 0
        worksheet.write(row, col, i)
        col += 1
    workbook.close()

 

生成计算题

标签:dom   lse   bsp   生成   creat   xls   yield   random   col   

原文地址:https://www.cnblogs.com/grub007/p/9800151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!