0

This is my Code:

seconds = 0
minuts = 0
while seconds > -1:
    rounded = round(seconds,2) 
    print(minuts,  'minutes,', rounded, 'seconds')
        
    seconds += 0.01
    time.sleep(0.01) 
    
    if rounded ==  59:
        s=0
        minuts+= 1

I want to press some key to stop the timer.

Srishruthik Alle
  • 500
  • 3
  • 14

1 Answers1

1

I have managed to make this work using the keyboard module in Python3 (detect key press in python?)

This program simply loops the timer while the space key is not pressed, and stops when it is.

import keyboard
import time

seconds = 0
minutes = 0

while not keyboard.is_pressed(" "):
    rounded = round(seconds, 2) 
    print(minutes,  'minutes,', rounded, 'seconds')

    seconds += 0.01
    time.sleep(0.01) 

    if rounded == 59:
        s = 0
        minutes += 1

If you need the keyboard module, use pip install keyboard from the command line to install it.

Pencilcaseman
  • 360
  • 6
  • 16
  • ModuleNotFoundError: No module named 'keyboard'. I did pip3 install keyboard. It still not working – Srishruthik Alle Jul 25 '20 at 19:06
  • So I did pip3 install keyboard 1 more time and it's saying Requirement already satisfied(Which I believe, it means it's already downloaded). And when I go to my IDE(Vscode). It's saying No module named keyboard – Srishruthik Alle Jul 25 '20 at 19:09
  • Did you use the vs code terminal panel? Just looking at this link (https://code.visualstudio.com/docs/languages/python) and wondered if that makes a difference? – Pencilcaseman Jul 25 '20 at 22:49
  • I used it on the terminal and the Vscode 'sTerminal. It still showing the same. I even restarted my computer and still had the same issue – Srishruthik Alle Jul 26 '20 at 01:51
  • I'm not sure what the problem could be then -- I tried it on my computer (PC) and it worked to install a new package and use it. This is probably a silly question but have you made sure to spell it correctly? For me intellisense does not show an error even if it is spelt incorrectly. – Pencilcaseman Jul 26 '20 at 08:59
  • Yes, I have spelled the import keyboard correctly and said pip3 install keyboard. I copied pasted your code above and It's still saying that. I mean, I don't know if it is a glitch or not... – Srishruthik Alle Jul 26 '20 at 18:12
  • That is very strange. Have you tried changing the environment? In VS Code it is in the bottom left corner where it says ```Python <32 or 64>-bit```. Maybe changing that will fix the issue. I have it on my actual python installation: i.e. ```c:\python38\python.exe``` – Pencilcaseman Jul 27 '20 at 10:32
  • Yes, I am running on Python 64-bit 3.7.6. I use Anaconda as my python installer. I also have python 64 bit 3.5.4 but it's still saying the same thing. I even created a Virtual Enviornment (Venv)but nope. This is really weird – Srishruthik Alle Jul 28 '20 at 00:24
  • Something strange has happen. I created a Venv with python 2.7.4 I am having no error and the I can't seem to stop the timer. I don't know if it is my fault or the python 2.7.4 and when I switch back to Python 3.7, it's showing that module error – Srishruthik Alle Jul 28 '20 at 00:26
  • I'm not entirely sure what would cause that to happen. Maybe it is worth posting another question? – Pencilcaseman Jul 28 '20 at 08:48