0

I'm new to programming in general and this is my first post here, so please explain like I'm five. This is a random string combination generator (sorry if I don't know how to call it properly) and I want to make the program start counting time since A key is first pressed until Q is pressed, without counting repeats caused by pressing A. I also know that this might come handy, but I don't really know how to make it work. Here's the code:

import random
import os
print('hello world')
while True:
    while True:
        key = msvcrt.getch().lower()
        if key == b'a':
            os.system('cls')

            alpha = random.randint(1, 2)
            bravo = random.randint(1, 2)
            if alpha == 1:
                print('foo')
            elif alpha == 2:
                print('bar')
            if bravo == 1:
                print('spam')
            elif bravo == 2:
                print('eggs')
        elif key == b'q':
            os.system('cls')
            print('elapsed time: mm:ss')   #this is where i want to have shown time elapsed from pressing A for the first time until pressing Q
            break
21214343
  • 1
  • 1
  • Does this answer your question? [How can I time a code segment for testing performance with Pythons timeit?](https://stackoverflow.com/questions/2866380/how-can-i-time-a-code-segment-for-testing-performance-with-pythons-timeit) – baduker Sep 16 '20 at 18:43

1 Answers1

0
  1. We create start uninitialized (as None) outside the loop
  2. When a is pressed, we check if start is uninitialized (if start is None)
    2.1. If it is uninitialized. we initialize with current time
  3. When q is pressed
    3.1. We check if start is initialized (if start is not None), (to cover the case that q is the first input so we won't do the next step if start is uninitialized)
    3.2. If start is initialized we print elapsed time
import random
import os
from timeit import default_timer as timer


print('hello world')
start = None
while True:
    while True:
        key = msvcrt.getch().lower()
        if key == 'a':
            # If start time is uninitialized
            if start is None:
                # Initialize start time
                start = timer()
            os.system('cls')
            alpha = random.randint(1, 2)
            bravo = random.randint(1, 2)
            if alpha == 1:
                print('foo')
            elif alpha == 2:
                print('bar')
            if bravo == 1:
                print('spam')
            elif bravo == 2:
                print('eggs')
        elif key == 'q':
            os.system('cls')
            if start is not None:
                end = timer()
                print(f'elapsed time: {end - start}')
            break
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
  • does not qoute work as i wished it would, i wouldn't like to change from msvcrt,getch() to input() because it would require me to press A and Enter right after, timing is crucial in what i'm aiming to do. Also, the {end - start} time apparently doesn't show the time from pressing A key for the very first time until loop broken with Q key, or so it seems to me. – 21214343 Sep 16 '20 at 19:07
  • @21214343 Changed back to `msvcrt.getch()`, and switched to another time measurement library – Aviv Yaniv Sep 16 '20 at 19:11
  • @21214343 `timeit` will produce far more accurate results since it will automatically account for things like garbage collection and OS differences – Aviv Yaniv Sep 16 '20 at 19:13
  • Now it doesn't work, hung on 'hello world'. Doesn't seem to respond in input. – 21214343 Sep 16 '20 at 19:22
  • Very strange, change to `key = input().lower()` just for the sake of testing it. it works fluently on my machine. @21214343 – Aviv Yaniv Sep 16 '20 at 19:24