codeFlowType:
codeWrite
codeFlowLang:
Python
Напишите тренажёр слепой печати. Для генерации случайной строки, используйте пакет lorem-text. Подробнее про Lorem Ipsum можете прочитать тут. Создайте обратный отсчёт "3... 2... 1... Начали!" с паузой в 1 секунду между цифрами, после которого на экране появится строка со случайным количеством слов - от 5 до 15. Задача пользователя напечатать без ошибок выведенную на экран строку. Ввод заканчивается нажатием Enter. Если строка напечатана без ошибок, пользователь видит сообщение со статистикой: затраченным временем и скоростью печати. Если же в строке были допущены опечатки, печатаем сообщение об ошибке. Далее предлагаем пользователю начать заново или завершить программу. Произведите декомпозицию задачи - создайте свои функции для отсчета времени, для работы с вводом пользователя, для подсчёта и отображения статистики и для основного кода программы. Использовать можно не более 4х импортированных пакетов
import time
import random
import keyboard
from lorem_text import lorem
def countdown():
for i in range(3, 0, -1):
print(i, end='... ')
time.sleep(1)
print("Начали!")
print()
def generate_random_text():
word_count = random.randint(5, 15)
random_text = lorem.words(wordcount=word_count)
return random_text
def get_user_input():
user_input = input("Введите текст: ")
return user_input
def calculate_stats(start_time, end_time, input_text, random_text):
elapsed_time = end_time - start_time
words_typed = len(input_text.split())
accuracy = calculate_accuracy(input_text, random_text)
typing_speed = words_typed / elapsed_time
print()
print("Статистика:")
print("Время выполнения:", round(elapsed_time, 2), "сек.")
print("Скорость печати:", round(typing_speed, 2), "слов/сек.")
print("Точность:", round(accuracy, 2))
def calculate_accuracy(input_text, random_text):
error_count = 0
input_words = input_text.split()
random_words = random_text.split()
for i in range(len(input_words)):
if input_words[i] != random_words[i]:
error_count += 1
accuracy = (len(input_words) - error_count) / len(input_words) * 100
return accuracy
def display_error_message():
print("Ошибка! Введенный текст содержит опечатки.")
def replay_program():
replay = input("Хотите начать заново? (Да/Нет): ")
if replay.lower() == 'да':
return True
else:
return False
def main():
while True:
countdown()
random_text = generate_random_text()
print(random_text)
start_time = time.time()
input_text = get_user_input()
end_time = time.time()
if input_text == random_text:
calculate_stats(start_time, end_time, input_text, random_text)
else:
display_error_message()
if not replay_program():
break
if __name__ == '__main__':
main()