Basic Python Programming Challenges

In this tutorial, we will create a Python quiz program that generates random arithmetic questions. The program asks users to solve basic math operations and provides a final score based on correct answers.

Challenge Overview

We need to build a program that:

  • Generates random arithmetic operations (+, -, *, /, //, %)
  • Takes user input for the number of questions
  • Evaluates user answers and provides feedback
  • Displays the final score

Complete Solution

Here's the complete arithmetic quiz program ?

# importing random and operator modules
import random
import operator

# main function
def main(n):
    print("Welcome to the quiz\nYou should answer floats upto 2 decimals")
    # initialising score to 0
    score = 0
    # loop to generate n questions
    for i in range(n):
        # getting answer and correctness of a question
        is_correct, answer = question(i)
        # checking whether is_correct is True or not
        if is_correct:
            # increment score by 1 if correct
            score += 1
            print('Correct Congrats!')
        else:
            # printing the correct answer
            print(f'Incorrect! Answer: {answer}')
    # displaying the total score
    print(f'Total score: {score}')

# function for the question
def question(n):
    # getting answer from the generate_function
    answer = generate_question()
    # taking answer from the user
    user_answer = float(input("Answer: "))
    # returning answer to the main function
    return user_answer == answer, answer

# function to generate a random question
def generate_question():
    # initialising operators for random generation
    operators = {
        '+' : operator.add,
        '-' : operator.sub,
        '*' : operator.mul,
        '/' : operator.truediv,
        '//' : operator.floordiv,
        '%' : operator.mod
    }
    # initialising numbers for expressions
    nums = [i for i in range(10)]
    # getting two random numbers from nums for calculation
    _1, _2 = nums[random.randint(0, 9)], nums[random.randint(0, 9)]
    # generating random operator from the list of operators
    symbol = list(operators.keys())[random.randint(0, 5)]
    # calculating the answer
    answer = round(operators.get(symbol)(_1, _2), 2)
    print(f'{_1} {symbol} {_2}?')
    return answer

if __name__ == '__main__':
    main(5)

Output

When you run the program, it generates 5 random questions ?

Welcome to the quiz
You should answer floats upto 2 decimals
5 + 7?
Answer: 12
Correct Congrats!
9 / 9?
Answer: 1
Correct Congrats!
4 + 7?
Answer: 11
Correct Congrats!
6 // 6?
Answer: 1.0
Correct Congrats!
9 % 3?
Answer: 0
Correct Congrats!
Total score: 5

How It Works

The program uses three main functions:

  • main(n): Controls the quiz flow and tracks the score
  • question(n): Handles user input and answer validation
  • generate_question(): Creates random arithmetic problems using the operator module

The operator module provides function equivalents for arithmetic operations, making it easy to select random operations programmatically.

Key Features

  • Supports six arithmetic operators: +, -, *, /, //, %
  • Uses numbers 0-9 for operands
  • Rounds division results to 2 decimal places
  • Provides immediate feedback after each question
  • Shows correct answer when user is wrong

Conclusion

This arithmetic quiz demonstrates random number generation, user input handling, and basic arithmetic operations in Python. You can enhance it by adding difficulty levels, time limits, or different number ranges for more challenging questions.

Updated on: 2026-03-25T06:41:11+05:30

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements