Python sprinting game using strings, loops and statements Here is my code for a sprinting game in Python. You have to tap the 'a' and 'd' keys as fast as you can to run 100 meters. Any improvements?

import msvcrt
import time
high_score = 50
name = "no-one"
while True:
    distance = int(0)
    print("\n--------------------------------------------------------------")
    print('\n\nWelcome to the 100m sprint, tap a and d rapidly to move!')    
    print('* = 10m')
    print("\n**Current record: " + str(high_score) + "s, by: " + name)
    print('\nPress enter to start')
    input()
    print('Ready...')
    time.sleep(1)
    print('GO!')
start_time = time.time()
while distance &lt; 100:           
    k1 = msvcrt.getch().decode('ASCII')
    if k1 == 'a':
        k2 = msvcrt.getch().decode('ASCII')
        if k2 == 'd':
            distance += 1
            if distance == 50:
                print("* You're halfway there!")
                elif distance % 10 == 0:
                    print('*')
fin_time = time.time() - start_time
fin_time = round(fin_time,2)
print('Well done you did it in...'+str(fin_time))
if fin_time &lt; high_score:
    print("Well done you've got a new high score ")
    name = input("Please enter your name : ")
Your code is very straightforward, which is good.



Use a conditional for printing values that don't exist yet

At the very top of your code, you are setting these two variables to default values:


high_score = 50
name = "no-one"



To me, it seems that your reason for this is so that, a few lines later when you are printing these values, you have some data to print.

However, since this is not actual data that means nothing for the user and you are using it as a placeholder, you can use a conditional when printing these messages:

if name == "":
    ... print high score ...


Then, you can set the default values to:


high_score = 0
name = ""



However, this would require later that you ensure that the user does not enter their username as "".



Store high score

Along with that above tip, what you could also try doing is storing the high score data. You don't need some file format. In fact, you could handle your own special type.

Here could be an example high score save/data file:

50
D.C.


where the first line has the completed time, and the second line has the user who achieved that time.

Then, in your script, you can simply read from the high score file.



Remove nesting

In your while distance &lt; 100 loop, you have quite a few levels of nesting. While the code in there isn't too complicated, it still is a bit harder on the eye.

To improve readability, try to keep everything on the same level (without breaking indentation rules):

while distance &lt; 100:           
    k = msvcrt.getch().decode('ASCII')
    if k != 'a':
        continue

    k = msvcrt.getch().decode('ASCII')
    if k != 'd':
        continue

    distance += 1
    if distance == 50:
        print("* You're halfway there!")
    elif distance % 10 == 0:
        print('*')


Note: I merged the two k1 and k2 variables into one since the k1 one is no longer needed when k2 is being checked.