使用Python生成Excel版口算题
疫情无法上班,在家实在宅的难受就自学了从论坛中下载的《用Python自动办公,做职场高手》,所练手制作了这个小程序。因为本人并非程序员出身而且编程的0基础,代码写的比较乱。也希望各位大神指正出代码的问题。
以下为代码,望指正
import xlsxwriter
import random
"""以下为可修改参数"""
"出题数目--100题整个一页A4纸大小"
t = 100
"出题列数"
m = 4
"出题难度大小"
n = 10
"出题规则
1、加法
2、减法
3、加减混合
4、乘法
5、除法
6、乘除混合
7、四则运算
"
g = 7
"""以下为函数代码,修改可能会无法使用"""
# 运算符
def fu(x):
sys = ['+', '-', '×', '÷']
if x == 1:
return sys
elif x == 2:
return sys
elif x == 3:
f = random.randint(0, 1)
return sys
elif x == 4:
return sys
elif x == 5:
return sys
elif x == 6:
f = random.randint(2, 3)
return sys
elif x == 7:
f = random.randint(0, 3)
return sys
# 随机数
def num(x):
y = random.randint(1, x)
return y
# 出题
def ti(x):
n1 = num(x)
n2 = num(x)
f = fu(g)
if f == '+':
s =
return s
elif f == '-':
n1, n2 = max(n1, n2), min(n1, n2)
s =
return s
elif f == '×':
s =
return s
elif f == '÷':
n1, n2 = max(n1, n2), min(n1, n2)
while n1 % n2 != 0:
n1 = num(x)
n2 = num(x)
n1, n2 = max(n1, n2), min(n1, n2)
s =
return s
# 四舍五入避免round(0.5)等于0
def sswr(x):
return int(x + 0.5)
# Excel创建表格操作
workbook = xlsxwriter.Workbook('./口算.xlsx')
sheet1 = workbook.add_worksheet('试题')
sheet2 = workbook.add_worksheet('答案')
sheet1.set_column('A:D', 21.6)
sheet1.set_row(30)
sheet2.set_column('A:D', 21.6)
sheet2.set_row(30)
# 写入格式
font = workbook.add_format({
'font_size': 20,
'font_name': '微软雅黑',
'bold': 1
})
# Excel写入操作
for h in range(sswr(t / m)):
for l in range(m):
s1 = ti(n)
shiti = str(s1) + str(s1) + str(s1) + '='
daan = str(s1) + str(s1) + str(s1) + '=' + str(s1)
sheet1.write(h, l, shiti, font)
sheet2.write(h, l, daan, font)
t = t - 1
if t == 0:
break
workbook.close()
起到凌晨 发表于 2020-7-11 02:02
runfile('D:/py/daxue.py', wdir='D:/py')
File "D:\py\daxue.py", line 12
"出题规则
把注释的方式换一下,别用一组双引号,用三组双引号,这个是注释出错了 runfile('D:/py/daxue.py', wdir='D:/py')
File "D:\py\daxue.py", line 12
"出题规则
^
SyntaxError: EOL while scanning string literal
同样报错 下载的是哪个帖子的视频哦 要是重新加入一列序号,显得的更加清晰 值得一试,谢谢分享 支持!!!!!! 谢谢分享 这是啥原因呢?求楼主解答 1e3e 发表于 2020-6-27 19:58
这是啥原因呢?求楼主解答
你看看是不是有些包没有安装 谢谢分享
页:
[1]
2