如何用键盘(ESC)结束循环?

时间:2018-04-01 04:06:48

标签: python

我希望用键盘描边结束while循环,但由于某种原因,循环完全跳过了循环的msvcrt部分。没有必要下载任何库,是否有更好的方法可以做到这一点?

代码:

import datetime
import time
import msvcrt

# import cv2

# from datetime import date, time

while True:

    # if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
    #   break

    name = input('\n' + "What is your name?")

    input('\n' + "Press any key to start the stopwatch")
    startTime = datetime.datetime.now()
    input('\n' + "Press any key to stop the stopwatch")
    endTime = datetime.datetime.now()

    time_out = ('\n' + "Time Out: " + str(endTime-startTime))
    print(time_out)

    student_out = ("Student Out: " + str(name))
    print(student_out)

    time_released = ("Time Released: " + str(startTime))
    print(time_released)

    time_back = ("Time Back: " + str(endTime))
    print(time_back)

    if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
        break

0 个答案:

没有答案