python打字练习(用python写一个供小孩子练习打字的脚本)

开始让小孩学习打字,每天打个五十个字,学习打字的同时,也可以写写日记,一举两得。

一开始我让小孩子学习打26个英文字母,没想到她说学校里有信息课,已经学过要怎么打,哪个手指控制哪几个键这些理论知识都已经有学过了。差的只是练习,于是我每天让她在Word上打26个字母,每天打十遍。打了几天发现这样不行,首先一直重复A到Z这样顺序的打,虽然速度加快了,可是在实际中的效果并不明显,再来是用Word打容易走神,打着打着就开始变换字体大小颜色背景等等。

于是给自己布置了一个小任务,写一个脚本:

1.纯黑的文字界面

2.随机生成一个字符串,以供打字。

3.在数据库中记录打的字,花费的时间,打错的次数等,并可供查询,以便确认是否有进步。

python打字练习(用python写一个供小孩子练习打字的脚本)(1)

目前给小孩测试了几天时间,效果不错,打字的速度也在每天提升当中。

代码分享如下:

import random import sqlite3 import datetime import sys class Typing: def __init__(self,length,times): self.length = length self.times = times self.conn = sqlite3.connect("words.db") self.cur = self.conn.cursor() def get_rand_words(self): """ 获取固定长度的随机字符串 """ text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"#abcdefghijklmnopqrstuvwxyz" res = "" for i in range(self.length): res = text[random.randint(0,len(text)-1)] return res def insert_data(self,date,words,used_time,wrong_time): """ 将数据存入数据库 """ self.cur.execute("create table if not exists data(日期 DATE,WORDS TEXT,用时 REAL,错误次数 REAL)") self.cur.execute("insert into data(日期,WORDS,用时,错误次数) values(?,?,?,?)",(date,words,used_time,wrong_time)) self.conn.commit() def run(self): """ 主要执行 """ left = self.times print(f"总共{left}个字符串等待输入") for i in range(self.times): wrong_time = 0 words = self.get_rand_words() start_time = datetime.datetime.now() check = True while check: print(words) input_words = input("请输入以上英文字母:") if not self.input_check(input_words,words): wrong_time = 1 print(f"输入错误,请重新输入...剩余:{left}个\n") else: left -= 1 print(f"输入正确...剩余:{left}个\n") check = False end_time = datetime.datetime.now() used_time =(end_time-start_time).seconds self.insert_data(datetime.datetime.today().strftime("%Y-%m-%d"),words,used_time,wrong_time) def input_check(self,input_words,words): """ 判断输入对错 """ if input_words == words: return True else: return False def query_data(self,fr_date,to_date): """ 查询起始时间内的数据 """ search_text = "select 用时,错误次数 from data where " if fr_date == to_date: search_text = f"日期 = '{fr_date}'" else: search_text = f"日期 >= '{fr_date}' AND 日期 <= '{to_date}'" self.cur.execute(search_text) res = self.cur.fetchall() self.conn.commit() times = 0 wrong_times = 0 if res: for i in range(len(res)): times = res[i][0] wrong_times = res[i][1] times /= len(res) if wrong_times: wrong_times/=len(res) return times,wrong_times def total(self): """ 统计数据并打印出来 """ S = "今天" now = datetime.datetime.today() today = now.strftime("%Y-%m-%d") last = self.last_day() if today != last: now = datetime.datetime.strptime(last,"%Y-%m-%d") today = last S = "最近一天" week = (now datetime.timedelta(-7)).strftime("%Y-%m-%d") month = (now datetime.timedelta(-30)).strftime("%Y-%m-%d") day_1 = self.query_data(today,today) day_7 = self.query_data(week,today) day_30 = self.query_data(month,today) print(f"{S}: ",end ="") print(f"平均用时:{round(day_1[0],2)}秒 平均错误次数:{round(day_1[1],2)}") print("最近一星期: ",end ="") print(f"平均用时:{round(day_7[0],2)}秒 平均错误次数:{round(day_7[1],2)}") print("最近一月 ",end ="") print(f"平均用时:{round(day_30[0],2)}秒 平均错误次数:{round(day_30[1],2)}") def last_day(self): self.cur.execute("select MAX(rowid) from data") res = self.cur.fetchone() self.cur.execute(f"select 日期 from data where rowid ='{res[0]}'") res2 = self.cur.fetchone() return res2[0] def query(self,fr_day,to_day): self.cur.execute(f"select WORDS from data where 日期 >= '{fr_day}' AND 日期 <= '{to_day}'") res = self.cur.fetchall() if res: for i in res: print(i[0]) else: print("没有查询到数据!") def quit(self): self.cur.close() self.conn.commit() sys.exit() if __name__ == "__main__": Type = Typing(5,50) while True: print(""" 1.开始打字练习 2.获取统计数据 3.查询输入内容 4.退出 请选择:""",end ="") choice = int(input()) if choice == 1: Type.run()#字符串长度、字符串个数 print("今天的任务已经完成,明天继续加油!") elif choice == 2: Type.total() elif choice == 3: fr_day = input("请输入开始日期:") to_day = input("请输入结束日期:") Type.query(fr_day,to_day) elif choice == 4: Type.quit() else: pass

需要可执行文件的也可以留下邮件,免费放送

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页