How can I build a counter in python?

前端 未结 3 751
甜味超标
甜味超标 2021-01-24 20:09

I want to have it so that each time I hit the space bar, the number in the terminal increases by one, so that I can keep a number in my head and not forget it. However, if I use

3条回答
  •  隐瞒了意图╮
    2021-01-24 20:53

    If you are using Linux/Unix, there is the curses module.

    import curses
    
    def check_press(scr):
        c = None
        x = 0
        while c != 120: # exit on x
            c = scr.getch()
            if c == 122: # count on 'z'
                x += 1
                scr.addstr(0, 0, "%5d" % x)
                scr.refresh()
    
    if __name__ == '__main__':
        curses.wrapper(check_press)
    

提交回复
热议问题