如何获取用户输入耗时并赋值给变量(打字游戏场景)
给打字游戏添加输入耗时统计功能
要实现统计用户输入每个单词的耗时(毫秒级),可以借助Python的time模块记录时间差,以下是修改后的完整代码:
from random import choice, randint import time # 导入时间模块 wrds = ['skateboard','mouse','river','simple','learn','sky','why','loser','lose','front','back','end','when','underline','flip','phone','puppet','doll','seven','wimpy','ski','charisma','wither','mine','craft','perish','list','python','name','age','climb','my','mine','plough','pull','zebra','cub','lion','zero','five','read','write','minus','plus','addition','China','Japan','united','states','of','and','will','America','north','south','west','east','bunny','rabbit','hare','plural','Persia','silent','loud','very','garlic','onion','tiger','specific','love','world','earth','murder','mud','identification','sign','maths','math','mathematics','science','scientific','history','geography','school','number','ten','french','speak','language','japanese','chinese','korean','Korea','bracket','square','circle','curly','triangle','hexagon','pentagon','shape','submissive','submit','male','female','gender','live','die','death','string','integer','plug','fraction','fact','opinion','mountain','type','typing','game','super','brother','sister','mother','mom','mama','father','dad','dada','papa','baby','family','you','try','except','that','myself','I','crime','criminal','jail','prison','fight','tell','talk','turn','curl','outlet','media','lap','top','dog','cat','cut','strategy','float','point','programmer','ritual','sinister','lover','kill','killer','knife','gun','half','shoot','president','presidency','promise','cling','cross','great','good','thumb','pinky','pink','blue','red','white','black','purple','pink','system','operate','language','spanish','flinch','ball','sphere','cube','cuboid','dinosaur','win','winner','silly','store','buy','home','make','house','clock','time','one','eleven','twelve','thirteen','teenager','young','old','book','electricity','motor','oil','wing','hub','colonel','star','sky','solar','eclipse','gum','eat','exam','assessment','study','pencil','eraser','sharpener','telephone','cell','honey','comb','bee','honeycomb','queen','prince','king','worker','trip','shrimp','innovate','creative','creat','screen','the','at','pie','trigonometry','geometry','algebra','basic','product','company','own','ownership','ship','relationship','oat','oatmeal','meal','bowl','plate','tab','goo','goop','bad','dab','lip','mouth','punch','bag','carry','care','an','stiff','still','it','thing','grip','happy','sad','drought','wet','cow','bull','sheep','embitterment','imprompt','impromptu','dilapidated','nest','bird','dream','egg','snake'] score = 0 multp = 1 word = choice(wrds) while True: # 记录输入开始时间 start_time = time.perf_counter() user_input = input(f'{word}: ') # 记录输入结束时间 end_time = time.perf_counter() # 计算耗时,转成毫秒并保留两位小数 input_time_ms = round((end_time - start_time) * 1000, 2) if user_input == word: score += multp multp = randint(1,3) word = choice(wrds) # 正确输入后显示耗时 print(f'输入耗时:{input_time_ms} 毫秒') else: print('\nAgain!\n') print(f'\nScore!: {score}\n')
关键修改说明
- 导入
time模块:用perf_counter()获取高精度时间,更适合短间隔时间测量 - 时间点记录:在提示输入前记起始时间,输入完成后记结束时间
- 耗时计算:通过时间差转换为毫秒,用
round()控制小数位数,结果存入input_time_ms变量,可用于分数计算、数据存储等场景
如果要优化分数逻辑(比如耗时越短加分越多),可以调整分数计算部分,示例:
# 耗时低于500毫秒额外加1分 score += multp + (1 if input_time_ms < 500 else 0)
内容的提问来源于stack exchange,提问作者anon
相关产品推荐
相关产品推荐

