Why is my variable not being incremented properly

  Kiến thức lập trình

I was working on a project for fun and the main function of the project is to take some information from one file and see if any of the vehicles match a plate on a different file and if they do then the program records the amount of expired vehicles based on their type. At the end the program launches a bar graph with the info.

This is the first file with vehicles on the highway (type – plate # – speed):
car – ABCD123 – 90
truck – XYZW456 – 100
bus – PQRS789 – 95
car – MNOP234 – 105
truck – UVWX567 – 88
bus – EFGH890 – 102

This is the plates that are in the expired file:
ABCD123

This is my code:
import matplotlib.pyplot as plt

def main():
“””
This function aims to read the file which has the list of highway vehicles
and take action
“””

expired_cars = 0
expired_trucks = 0
expired_buses = 0
speeds = []
plates = []
vehicles = []

with open('HighwayData.csv', 'r') as f:
    lines = f.readlines()

    for line in lines:
        info = line.strip().split('-')
        print("Info:", info)  # Debugging statement
        vehicle_type = info[0]
        plate = info[1]
        speed = info[2]

        """if check_expiry(plate) is True:
            if vehicle_type == 'truck':
                expired_trucks += 1
            elif vehicle_type == 'car':
                expired_cars += 1
                print("Expired Cars Count:", expired_cars)  # Debugging statement
            elif vehicle_type == 'bus':
                expired_buses += 1"""

        speeds.append(int(speed))
        plates.append(str(plate))
        vehicles.append(str(vehicle_type))

for i in range(len(plates)):
    if check_expiry(plates[i]):
        print(vehicles[i])
        if vehicles[i] == "truck":
            expired_trucks += 1
        elif vehicles[i] == "car":
            expired_cars += 1
        elif vehicles[i] == "bus":
            expired_buses += 1


average = find_average_speed(speeds)

print("Expired Cars:", expired_cars)  # Debugging statement
print("Expired Trucks:", expired_trucks)  # Debugging statement
print("Expired Buses:", expired_buses)  # Debugging statement
print("Average Speed:", average)  # Debugging statement


make_graph([expired_trucks, expired_cars, expired_buses, average])

def check_expiry(plate: str) -> bool:
“””
This function aims to check if the vehicle has a expired plate or not
“””
with open(‘ExpiredPlate.csv’, ‘r’) as s:
lines = s.readlines()

    for line in lines:
        info = line.strip()
        print("Plate in file:", info)  # Debugging statement


        if plate.strip() == info:
            print("Plate matched:", plate)  # Debugging statement
            return True
        else:
            print("Plate not matched:", plate)  # Debugging statement


return False

def find_average_speed(speeds_list: list[int]) -> int:
“””
This function aims to calculate the average speed
“””
total = 0
for i in speeds_list:
total += i

average_speed = total // len(speeds_list)

return average_speed

def make_graph(data):
‘(x: list[str], y: list[int])’
“””
This function aims to launch the graph of the data which is read from the file
“””

x_values = ['truck', 'car', 'bus', 'average speed']
#y_values = [expired_trucks, expired_cars, expired_buses, average]

plt.bar(x_values, data)

plt.xlabel('Vehicle Types')
plt.ylabel('Expired Vehicles')
plt.title('Number of Expired Vehicles On Highway')

plt.show()

if name == “main“:
main()

I have tried many different approaches such as putting into a list and the one I have commented but every time the expired is 0 and when outputted the programs says no expired trucks (that’s right), no expired cars (that’s wrong it should be 1), no expired buses (that’s right), and the speed is calculated and graphed properly.

I have tried to match the plates by putting them into a list and trying them separately and what I find is that the program matches the plates correctly but doesn’t update. Also there are some debugging statements in the code.

New contributor

Gaming Titan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT