Python Typing Test

Master your coding speed with Python-syntax exercises.

Loading test...
1:00

Test Results

0
WPM
0%
Accuracy
0
CPM
0
Errors
"Decorators modify function behavior without changing code", "Generators create iterators with yield statements", "Context managers handle resource allocation", "Metaclasses customize class creation behavior", "Descriptors manage attribute access in classes", "Asyncio enables asynchronous programming", "Type hints improve code documentation", "Dunder methods enable operator overloading", "Python's GIL affects multi-threading performance", "Abstract base classes define interfaces" ], python: [ "def calculate_average(numbers): return sum(numbers)/len(numbers)", "for i in range(10): print(i**2) if i%2==0 else print(i)", "[x for x in range(100) if x%3==0 and x%5==0]", "with open('data.txt') as f: contents = f.read()", "import math; hypotenuse = math.sqrt(a**2 + b**2)", "class Rectangle: def __init__(self,w,h): self.width=w; self.height=h", "try: result = x/y except ZeroDivisionError: print('Cannot divide by zero')", "@app.route('/') def home(): return render_template('index.html')", "df = pd.DataFrame(data={'col1': [1,2], 'col2': [3,4]})", "async def fetch_data(): async with aiohttp.ClientSession() as session: pass" ] }; // DOM elements const wordsDisplay = document.getElementById('wordsDisplay'); const typingInput = document.getElementById('typingInput'); const timerElement = document.getElementById('timer'); const startBtn = document.getElementById('startBtn'); const newTestBtn = document.getElementById('newTestBtn'); const tryAgainBtn = document.getElementById('tryAgainBtn'); const resultsDiv = document.getElementById('results'); const difficultyButtons = document.querySelectorAll('.difficulty-btn'); // Stats elements const wpmElement = document.getElementById('wpm'); const accuracyElement = document.getElementById('accuracy'); const cpmElement = document.getElementById('cpm'); const errorsElement = document.getElementById('errors'); // Test variables let timeLeft = 60; let timer; let isTestRunning = false; let currentDifficulty = 'easy'; let words = []; let currentWordIndex = 0; let correctChars = 0; let totalChars = 0; let errors = 0; let startTime, endTime; // Initialize the test function initTest() { // Reset values timeLeft = 60; timerElement.textContent = '1:00'; typingInput.value = ''; typingInput.disabled = false; typingInput.focus(); isTestRunning = true; startBtn.disabled = true; newTestBtn.disabled = true; resultsDiv.classList.remove('active'); // Get words based on difficulty words = getRandomWords(currentDifficulty); currentWordIndex = 0; correctChars = 0; totalChars = 0; errors = 0; // Display words displayWords(); // Start timer startTime = new Date(); timer = setInterval(updateTimer, 1000); } // Get random words for the selected difficulty function getRandomWords(difficulty) { const allWords = content[difficulty]; const shuffled = [...allWords].sort(() => 0.5 - Math.random()); return shuffled.slice(0, 10); // Use 10 phrases for the test } // Display words in the test area function displayWords() { wordsDisplay.innerHTML = ''; words.forEach((word, index) => { const wordSpan = document.createElement('span'); wordSpan.className = 'word'; wordSpan.textContent = word + ' '; wordSpan.id = `word-${index}`; wordsDisplay.appendChild(wordSpan); }); // Highlight first word document.getElementById(`word-${currentWordIndex}`).classList.add('current'); } // Update timer countdown function updateTimer() { timeLeft--; const minutes = Math.floor(timeLeft / 60); const seconds = timeLeft % 60; timerElement.textContent = `${minutes}:${seconds < 10 ? '0' : '' }${seconds}`; if (timeLeft <=0) { endTest(); } } // End the test function endTest() { clearInterval(timer); isTestRunning=false; typingInput.disabled=true; startBtn.disabled=false; newTestBtn.disabled=false; endTime=new Date(); // Calculate results calculateResults(); // Show results resultsDiv.classList.add('active'); } // Calculate test results function calculateResults() { const timeInMinutes=(endTime - startTime) / 1000 / 60; const wordsTyped=correctChars / 5; // Standard word length is 5 chars const wpm=Math.round(wordsTyped / timeInMinutes); // Calculate accuracy const accuracy=Math.round((correctChars / totalChars) * 100) || 0; // Calculate characters per minute const cpm=Math.round(totalChars / timeInMinutes); // Display results wpmElement.textContent=wpm; accuracyElement.textContent=accuracy + '%' ; cpmElement.textContent=cpm; errorsElement.textContent=errors; } // Event listeners startBtn.addEventListener('click', initTest); newTestBtn.addEventListener('click', ()=> { words = getRandomWords(currentDifficulty); currentWordIndex = 0; displayWords(); typingInput.value = ''; }); tryAgainBtn.addEventListener('click', () => { resultsDiv.classList.remove('active'); initTest(); }); // Handle typing input typingInput.addEventListener('input', (e) => { if (!isTestRunning) return; const currentWord = words[currentWordIndex]; const inputValue = typingInput.value.trim(); // Check if current word is completed if (inputValue === currentWord) { // Mark word as correct document.getElementById(`word-${currentWordIndex}`).classList.remove('current'); document.getElementById(`word-${currentWordIndex}`).classList.add('correct'); // Update stats correctChars += currentWord.length; totalChars += currentWord.length; // Move to next word currentWordIndex++; typingInput.value = ''; // Check if test is complete if (currentWordIndex >= words.length) { words = getRandomWords(currentDifficulty); currentWordIndex = 0; displayWords(); } else { // Highlight next word document.getElementById(`word-${currentWordIndex}`).classList.add('current'); } } else { // Check for errors if (inputValue !== currentWord.substring(0, inputValue.length)) { errors++; document.getElementById(`word-${currentWordIndex}`).classList.add('incorrect'); } else { document.getElementById(`word-${currentWordIndex}`).classList.remove('incorrect'); } // Update total chars (for CPM calculation) totalChars = correctChars + inputValue.length; } }); // Handle difficulty selection difficultyButtons.forEach(button => { button.addEventListener('click', () => { difficultyButtons.forEach(btn => btn.classList.remove('active')); button.classList.add('active'); currentDifficulty = button.dataset.difficulty; if (!isTestRunning) { words = getRandomWords(currentDifficulty); currentWordIndex = 0; displayWords(); } }); }); // Mobile menu toggle const hamburger = document.querySelector('.hamburger'); const nav = document.querySelector('nav'); hamburger.addEventListener('click', () => { nav.classList.toggle('active'); }); // Initialize with easy words words = getRandomWords('easy'); displayWords();