Part of the series

Several example codes

~2 min read · Updated Sep 17, 2025

Program Overview

This Python program simulates a traffic violation system.
There are 10 types of violations, each assigned a code from 0 to 9.
Each code has a specific fine amount.
For each vehicle, the program reads:
- License plate number
- Number of violations
- Violation codes
It calculates the total fine and displays it.
Entering -999 as the license plate number will terminate the program.


Python Code:


# Define fine amounts for violation codes 0 to 9
penalties = [100000, 200000, 150000, 300000, 250000, 180000, 220000, 270000, 160000, 190000]

while True:
    car_number = input("Enter vehicle license plate (enter -999 to exit): ")
    if car_number == "-999":
        print("Exiting program.")
        break

    count = int(input("Enter number of violations for this vehicle: "))
    total_fine = 0

    for i in range(count):
        code = int(input(f"Enter violation code #{i+1} (0 to 9): "))
        if 0 <= code <= 9:
            total_fine += penalties[code]
        else:
            print("Invalid violation code. Ignored.")

    print(f"Total fine for vehicle {car_number}: {total_fine} Rials\n")

Sample Output:


Enter vehicle license plate (enter -999 to exit): 45D123  
Enter number of violations for this vehicle: 3  
Enter violation code #1 (0 to 9): 2  
Enter violation code #2 (0 to 9): 5  
Enter violation code #3 (0 to 9): 7  
Total fine for vehicle 45D123: 600000 Rials

Enter vehicle license plate (enter -999 to exit): -999  
Exiting program.

Explanation:

Here’s how the program works:
- A list penalties stores fine amounts for each violation code
- The program reads vehicle data and violation codes
- It calculates the total fine by summing the corresponding penalty values
- If the user enters -999, the program exits


Written & researched by Dr. Shahin Siami

Related Articles

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

Continue

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

Continue

Several example codes

understand the Python programming language

Continue

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

Continue

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

Continue

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

Continue