My Plates
In NSW, home to Killarney Heights High School, it’s possible to request custom license plate for your vehicle, with your choice of letters and numbers instead of random ones. There are few requirements for custom license plates apart from offensive words and profanity.
Some countries have requirements like the following:
- "All plates must start with at least two letters."
- “plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.”
- “Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable plate; AAA22A would not be acceptable. The first number used cannot be a ‘0’.”
- “No periods, spaces, or punctuation marks are allowed.”
In plates.py
, implement a program that prompts the user for a number plate and then output Valid
if meets all of the requirements or Invalid
if it does not. Assume that any letters in the user’s input will be uppercase. Structure your program per the below, wherein is_valid
returns True
if s
meets all requirements and False
if it does not. Assume that s will be a str
. You’re welcome to implement additional functions for is_valid
to call (e.g., one function per requirement).
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
...
main()
Hints
- Recall that a
str
comes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods. - Much like a
list
, astr
is a “sequence” (of characters), which means it can be “sliced” into shorter strings with syntax likes[i:j]
. For instance, ifs
is"CS50"
, thens[0:2]
would be "CS".
Before You Begin
From the root of your repository execute cd src/loops
So your current working directory is ...
plates.py
where you’ll write your program.
How to Test
Here’s how to test your code manually:
- Run your program with
python plates.py
. TypeCS50
and press Enter. Your program should output: - Run your program with
python plates.py
. TypeCS05
and press Enter. Your program should output: - Run your program with
python plates.py
. TypeCS50P
and press Enter. Your program should output - Run your program with
python plates.py
. TypePI3.14
and press Enter. Your program should output - Run your program with
python plates.py
. TypeH
and press Enter. Your program should output - Run your program with
python plates.py
. TypeOUTATIME
and press Enter. Your program should output
Pytest
You can execute the below to check your code using pytest
from the root directory.
A green output from running the test means it was successful. A red output means there is a bug in your code that you need to fix.
How to Submit
From github desktop or the command line, commit your changes and push them to your repository.
Codespaces
If you are using codespaces, you can commit your changes directly from the Codespace interface. Click on the Source Control icon in the left sidebar, then click on the "..." button and select "Commit to main". Enter a commit message and click "Commit".
Codespace terminal or your local terminal.
Note
You will need to have installed git-scm
for this to work locally
At the /datatypes $
prompt in your terminal:
Note
Remember to replace "your message here" with a meaningful commit message that describes your changes.