计时器不在python 2.7中工作

时间:2018-02-21 11:43:18

标签: python python-2.7 timer

我正在使用Python 2.7进行一些家庭编码,我无法弄清楚为什么这段代码不起作用。我尝试它并且它工作正常,但是说我在-0.000938375024452秒做了... 任何帮助非常感谢

import timeit
import time
alphabet = "abcdefghijklmnopqrstuvwxyz"
again = "yes"
print "Hi."
time.sleep(1)
print "How quick can you type the alphabet?"
time.sleep(1)
print "Well you're about to find out..."
while "YES" in again.upper():
    time.sleep(2)
    begin = raw_input("When you are ready, press enter and type the alphabet.")
    start = timeit.timeit()
    attempt1 = raw_input("")
    end = timeit.timeit()
    time1 = end - start
    if attempt1.upper() == alphabet.upper():
        print "Ok. You did it in..."
        time.sleep(1)
        print time1,"seconds!"
        time.sleep(1)
        again = raw_input("Would you like another go?")
    else:
        print "Sorry, but that was incorrect."
        time.sleep(1)
        print "Try again."
        again = "yes"

1 个答案:

答案 0 :(得分:1)

timeit.timeit()不会为您返回时间戳。它运行pass 1000000次,并返回它的持续时间。

Docs for timeit

改为使用time.time()

time.time()