Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
2
Indexable
Never
import random

def times_table_game():
    # Ask the user which times table they want to practice
    times_table = int(input("Which times table do you want to practice? Enter a number from 1 to 10: "))
    
    # Set the number of questions and initialize the score
    num_questions = 10
    score = 0
    
    # Loop through the questions
    for i in range(num_questions):
        # Generate a random number between 1 and 10
        num1 = random.randint(1, 10)
        
        # Calculate the answer
        answer = num1 * times_table
        
        # Ask the user for their answer
        user_answer = int(input(f"{num1} x {times_table} = "))
        
        # Check if the answer is correct
        if user_answer == answer:
            print("Correct!")
            score += 1
        else:
            print(f"Incorrect. The correct answer is {answer}.")
    
    # Display the final score
    print(f"You got {score} out of {num_questions} questions correct.")
    
# Call the times_table_game function to start the game
times_table_game()