First off, variables and functions should be in
PascalCase. Only classes should be in
PascalCase. If you have a variable with an unchangining value, (constant), it should be in
Secondly, I’m noticing that you’re putting variables in parentheses in a lot of your expressions. For example,
Speed = (Distance) / (TimeTaken) can become
speed = distance / time_taken, or
if (Speed) > (Limit) can become
if speed > speed_limit.
You should be using context managers to manage files. When you just use
open(), and then close it using
.close(), certain memory issues can occur. The proper way to open a file can be seen in the below example:
with open("path/to/myfile", "w") as file_name: # Do stuff with file_name
Using this method, there’s no need to call
.close() on the file, and the file is closed explicitly, so you don’t need to worry about memory issues.
I notice that you repeat the
Speed = (Distance) / (TimeTaken) a lot. Preferably, when you have repetition like this in your code, you can shorten it into a function. In this case, I’d create the following function:
def calculate_speed(distance, time_taken): return distance / time_taken
Finally, add some comments to this code. You don’t need comments everywhere, but you should add some in places where your code may be slightly harder to understand just by reading it.
For any style guide tips, see Python’s official style guide, PEP8.
I’m just going to focus on the function
NumPlate, because otherwise I’d be writing a novel in this answer.
First: It’s defined in a weird place. Is there any particular reason why you only want it to exist if
Speed > Limit?
Second: Why is the only argument
self when it’s not an instance method?
Third: I cannot for the life of me tell what this function is supposed to do. Right now, you have
NumPlate = str(input("Enter the vehicle's number plate.")), which makes me suspect that the user is supposed to enter in a plate number, but then you have all of that randomization stuff as well.
Fourth: As is, that
for loop is effectively useless. The first letter of
plateFormat is defined as
'L', so the loop hits the first
if block, skips the
elif block, hits that
break statement, and exits the function (returning
None because it never hit a return statement).