|
| 1 | +# Hi, this code is written by Purna Shrestha. |
| 2 | +import random |
| 3 | +import time |
| 4 | +from colorama import Fore, Style |
| 5 | +# Please install colorama to enable colorful output. You can install it with |
| 6 | +# 'pip install colorama' command on the terminal. |
| 7 | + |
| 8 | +# Additional prompts |
| 9 | +prompts = [ |
| 10 | + "Hi, this code is written by Purna Shrestha.", |
| 11 | + "The quick brown fox jumps over the lazy dog", |
| 12 | + "Python is a versatile and powerful programming language", |
| 13 | + "Coding is a skill that can change your life", |
| 14 | + "Practice makes perfect", |
| 15 | + "All for one, and one for all.", |
| 16 | + "The only limit is your imagination.", |
| 17 | + "Success is walking from failure to failure with no loss of enthusiasm.", |
| 18 | +] |
| 19 | + |
| 20 | +def display_prompt(prompt): |
| 21 | + print("Type this: ", prompt, "") |
| 22 | + input("Press ENTER when you are ready!") |
| 23 | + |
| 24 | +def calculate_typing_speed(prompt, start_time, end_time): |
| 25 | + words = len(prompt.split()) |
| 26 | + elapsed_time = end_time - start_time |
| 27 | + speed = words / (elapsed_time / 60) |
| 28 | + return speed |
| 29 | + |
| 30 | +def play_typing_game(): |
| 31 | + prompt = random.choice(prompts) |
| 32 | + display_prompt(prompt) |
| 33 | + |
| 34 | + start_time = time.time() |
| 35 | + user_input = input() |
| 36 | + end_time = time.time() |
| 37 | + |
| 38 | + if user_input == prompt: |
| 39 | + print(Fore.GREEN + "Congratulations! You typed it correctly.") |
| 40 | + print(Style.RESET_ALL) # Reset colorama settings |
| 41 | + else: |
| 42 | + print(Fore.RED + "Sorry, there were errors in your typing:") |
| 43 | + for i in range(len(prompt)): |
| 44 | + if user_input[i:i+1] == prompt[i:i+1]: |
| 45 | + print(Fore.GREEN + user_input[i:i+1], end='') |
| 46 | + else: |
| 47 | + print(Fore.RED + user_input[i:i+1], end='') |
| 48 | + print(Style.RESET_ALL) # Reset colorama settings |
| 49 | + |
| 50 | + typing_speed = calculate_typing_speed(prompt, start_time, end_time) |
| 51 | + print("Your typing speed: {:.2f} words/minute".format(typing_speed)) |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + while True: |
| 55 | + play_typing_game() |
| 56 | + play_again = input("Play again? (yes/no): ").strip().lower() |
| 57 | + if play_again != 'yes': |
| 58 | + break |
0 commit comments