利用random生成随机数,转换成该ascii码对应的字符输出在屏幕上,通过读取用户输入进行比对,地出正确率
[root@foundation27 ~]# vim typewriting.py
#!/bin/bash/python
#coding=utf-8
from __future__ import division
import random
print "**********欢迎来到 Mr.Fang 开发的打字练习软件,祝您玩的愉快!**********\n"
while True:
flag=raw_input("继续(C)? 任意其他字符退出 : ")
flag=flag.lower()
if flag=="c":
indicator=random.randint(3,30)
example_list=[]
example_str=""
for i in xrange(indicator):
num=random.randint(65,122)
# while num >= 91 and num <= 96:
# num=random.randint(65,122)
example_list.append(chr(num))
example_str="".join(example_list)
print "请快速输入以下",indicator,"个字母:",example_str
user_input=raw_input("请输入:")
if len(user_input)==0:
print "输入为空,得了个鸡蛋OOOO"
else:
Correct_Num=0
myrange=min(len(example_str),len(user_input))
for j in xrange(myrange):
if user_input[j]==example_str[j]:
Correct_Num+=1
if len(user_input)>indicator:
print "输入超限,扣10分,本次得分:%.2f"%((Correct_Num/indicator)*100-10)
elif Correct_Num==indicator:
print "完全正确,非常棒!"
else:
print "本次得分:%.2f"%((Correct_Num/indicator)*100)
else:
break
[root@foundation27 ~]# python typewriting.py