大家好!
这是我第一次发博客,如有优化空间,请各位在评论区大胆地提出建议!我会虚心接受的!
前言
最近一段时间,我亲爱的弟弟迷上了做数学题,天天缠着我妈出题给他,我妈被他搞得焦头烂额,便向我求助。我自告奋勇,要用Python帮她解决!
准备
解释器:Python 3.9.6(版本都没关系,是Python3.x就行)
IDE:VSCode
库:random、datetime、rich(为了美观)、pyinstaller
rich和pyinstaller是第三方库,所以要先在cmd中输入:
pip install rich
pip install pyinstaller
请一定要先看这篇文章来掌握rich库的用法。
等库下载完毕,就可以导入进去了。
import random
import datetime
from rich import print
导完了库,就可以开始掉头发写代码了
第一部分:初始化
我要在最后进行一个复盘,所以在前面先设好:
rightTimes = 0#答对题目数量
a = 0#数1
sign1 = 0#符号1
b = 0#数2
sign2 = 0#符号2
c = 0#数3
answer_right = 0#正确答案
answer_input = 0#输入答案
然后询问:
print("[bold magenta]请输入出题范围:[/bold magenta]",end = "")
size = int(input())
print("[bold magenta]请输入题目数量:[/bold magenta]",end = "")
times = int(input())
(因为rich我只导了print函数,所以input显示不出print可以显示出的漂亮的文本,我只能将print和input分开写)
这样,初始化就完毕了。
第二部分:创建算式
首先,我们要确定这个式子是两个数的还是三个数的:
control = random.randint(0,1)#控制变量control, 通过0和1来确定该算式为两数计算(0)还是三数计算(1)
然后生成数和符号:
a = random.randint(0,size)
sign1 = random.randint(0,1)
if sign1 == 0:
sign1 = "+"
else:
sign1 = "-"
if sign1 == "-":
if a == 0:
b = 0
else:
b = random.randint(0,a)
else:
b = random.randint(0,size)
if control == 1:
sign2 == random.randint(0,1)
if sign2 == 0:
sign2 = "+"
else:
sign2 = "-"
if sign1 == "-" and sign2 == "-":#控制c的范围
c = random.randint(0,a-b)
elif sign1 == "-" and sign2 == "+":
c = random.randint(0,size)
elif sign1 == "+" and sign2 == "-":
if a+b > size:
c = random.randint(0,size)
else:
c = random.randint(0,a+b)
else:
c = random.randint(0,size)
c的范围我现在自己也理不大清楚了,你们自己去琢磨吧 唉……ε=(´ο`*)))(别骂我!)
随后去生成答案:
answer_right = a
if sign1 == "+":
answer_right += b
else:
answer_right -= b
if control == 1:
if sign2 == "+":
answer_right += c
else:
answer_right -= c
这块应该很好理解,就是顺序向下,如果是加号则加上下一个数,如果是减号则减掉下一个数。
至此,算式创建就正式结束了
第三部分:输入判断
首先,我们要将算式输出:
print(i+1,end = "")
print("[bold blue]|[/bold blue]",end = "")
print(a,end = "")
print(sign1,end = "")
print(b,end = "")
if control == 1:
print(sign2,end = "")
print(c,end = "")
print("=",end = "")
随后判断输入是否符合逻辑(因为我的弟弟总是输入一些奇奇怪怪的答案)。
首先,我要建立两个(我认为是)布尔值变量:
unRight = True
enterFirst = False
然后:
while unRight:
answer_input = input()
if answer_input == '\n':
print("请不要输入回车! 请再次输入!")
unRight = True
enterFirst = True
if not enterFirst:
if answer_input[0] == '0' and answer_input[1] != '.':
print("请不要在开头输入0! 请再次输入!")
unRight = True
else:
unRight = False
if not unRight and not enterFirst:
try:
answer_input = int(answer_input)
unRight = False
except:
print("请不要输入字符! 请再次输入!")
unRight = True
然后,如果输入错误,就再将式子输出一遍:
if unRight:
print(i+1,end = "")
print("[bold blue]|[/bold blue]",end = "")
print(a,end = "")
print(sign1,end = "")
print(b,end = "")
if control == 1:
print(sign2,end = "")
print(c,end = "")
print("=",end = "")
如果输入正确,unRight变成false,就会退出循环,然后判断正误:
if(answer_input == answer_right):
print("[bold green]正确[/bold green]")
rightTimes += 1
else:
print("[bold red]错误[/bold red]")
第四部分:复盘、退出
直接上代码:
end = datetime.datetime.now()
print("你一共答了",end = "")
print(times,end = "")
print("题, 答对了",end = "")
print(rightTimes,end = "")
print("题, 答错了",end = "")
print(times-rightTimes,end = "")
print("题, 共用时",end = "")
if end.hour - end.hour != 0:
print(end.hour - start.hour,end = "")
print("小时",end = "")
print(end.minute - start.minute,end = "")
print("分钟",end = "")
if end.minute > start.minute:
print(end.second - start.second,end = "")
print("秒。",end = "")
elif end.hour - start.hour == 0 and end.minute - start.minute != 0:
print(end.minute - start.minute,end = "")
print("分钟",end = "")
if end.minute > start.minute:
print(end.second - start.second,end = "")
print("秒。",end = "")
elif end.hour - start.hour == 0 and end.minute - start.minute == 0 and end.second - start.second != 0:
print(end.second - start.second,end = "")
print("秒。",end = "")
print()
if rightTimes == times:
print("[bold green]你真厉害![/bold green]")
elif float(times*0.4) <= rightTimes < times:
print("[bold yellow]很不错哦![/bold yellow]")
else:
print("[bold red]还需努力![/bold red]")
print()
print("----------------------------------------------------------------")
print("程序已结束")
最后,插入一个断点:
Stop = input()
至此,程序全部结束。
完整代码
import random
import datetime
from rich import print
rightTimes = 0#答对题目数量
a = 0#数1
sign1 = 0#符号1
b = 0#数2
sign2 = 0#符号2
c = 0#数3
answer_right = 0#正确答案
answer_input = 0#输入答案
print("[bold magenta]请输入出题范围:[/bold magenta]",end = "")
size = int(input())
print("[bold magenta]请输入题目数量:[/bold magenta]",end = "")
times = int(input())
print()
print("----------------------------------------------------------------")
print("开始答题")
start = datetime.datetime.now()
for i in range(times):
control = random.randint(0,1)#控制变量control, 通过0和1来确定该算式为两数计算(0)还是三数计算(1)
a = random.randint(0,size)
sign1 = random.randint(0,1)
if sign1 == 0:
sign1 = "+"
else:
sign1 = "-"
if sign1 == "-":
if a == 0:
b = 0
else:
b = random.randint(0,a)
else:
b = random.randint(0,size)
if control == 1:
sign2 == random.randint(0,1)
if sign2 == 0:
sign2 = "+"
else:
sign2 = "-"
if sign1 == "-" and sign2 == "-":
c = random.randint(0,a-b)
elif sign1 == "-" and sign2 == "+":
c = random.randint(0,size)
elif sign1 == "+" and sign2 == "-":
if a+b > size:
c = random.randint(0,size)
else:
c = random.randint(0,a+b)
else:
c = random.randint(0,size)
answer_right = a
if sign1 == "+":
answer_right += b
else:
answer_right -= b
if control == 1:
if sign2 == "+":
answer_right += c
else:
answer_right -= c
print(i+1,end = "")
print("[bold blue]|[/bold blue]",end = "")
print(a,end = "")
print(sign1,end = "")
print(b,end = "")
if control == 1:
print(sign2,end = "")
print(c,end = "")
print("=",end = "")
unRight = True
enterFirst = False
while unRight:
answer_input = input()
if answer_input == '\n':
print("请不要输入回车! 请再次输入!")
unRight = True
enterFirst = True
if not enterFirst:
if answer_input[0] == '0' and answer_input[1] != '.':
print("请不要在开头输入0! 请再次输入!")
unRight = True
else:
unRight = False
if not unRight and not enterFirst:
try:
answer_input = int(answer_input)
unRight = False
except:
print("请不要输入字符! 请再次输入!")
unRight = True
if unRight:
print(i+1,end = "")
print("[bold blue]|[/bold blue]",end = "")
print(a,end = "")
print(sign1,end = "")
print(b,end = "")
if control == 1:
print(sign2,end = "")
print(c,end = "")
print("=",end = "")
if(answer_input == answer_right):
print("[bold green]正确[/bold green]")
rightTimes += 1
else:
print("[bold red]错误[/bold red]")
end = datetime.datetime.now()
print("你一共答了",end = "")
print(times,end = "")
print("题, 答对了",end = "")
print(rightTimes,end = "")
print("题, 答错了",end = "")
print(times-rightTimes,end = "")
print("题, 共用时",end = "")
if end.hour - end.hour != 0:
print(end.hour - start.hour,end = "")
print("小时",end = "")
print(end.minute - start.minute,end = "")
print("分钟",end = "")
if end.minute > start.minute:
print(end.second - start.second,end = "")
print("秒。",end = "")
elif end.hour - start.hour == 0 and end.minute - start.minute != 0:
print(end.minute - start.minute,end = "")
print("分钟",end = "")
if end.minute > start.minute:
print(end.second - start.second,end = "")
print("秒。",end = "")
elif end.hour - start.hour == 0 and end.minute - start.minute == 0 and end.second - start.second != 0:
print(end.second - start.second,end = "")
print("秒。",end = "")
print()
if rightTimes == times:
print("[bold green]你真厉害![/bold green]")
elif float(times*0.4) <= rightTimes < times:
print("[bold yellow]很不错哦![/bold yellow]")
else:
print("[bold red]还需努力![/bold red]")
print()
print("----------------------------------------------------------------")
print("程序已结束")
Stop = input()
打包
最后,我们使用pyinstaller将这个Python文件打包成exe:
我将Python文件命名成了Main.py,并配置好了图标(icon.ico)
打开cmd,使用cd命令工作目录调到该Python文件的位置,然后执行:
pyinstaller -D -i icon.ico -n Main_App Main.py
(关于pyinstaller的更多用法,请参考这篇文章)
效果图
后续
弟弟玩的非常开心,妈妈也直夸我会学以致用!