mercredi 11 décembre 2019

Python Adding function with string slicing

def main():
    userInput = input("Enter license plate tag: ")
    while(not validateTag(userInput)):
        print("INVALID ID: Try again")
        userInput = input("Please reenter your license plate tag: ")
    for char in userInput:
        charToWord(char)




def charToWord(char):
    nato = ["Alpha","Beta", "Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India","Juliett","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor","Whiskey","X-Ray","Yankee","Zulu" ]
    word = ""


    charNum = ord(char.upper()) - 65
    word = nato[charNum]


    if char.isalpha():
        print(word)
    elif char == '9':
        print("Niner")
    elif char == "-":
        print("Dash")
    else:
        print(char)

    return word

def validateTag(userInput):
    isValid = True

    for currChar in userInput:
        if not currChar.isalnum() and currChar != "-":
            isValid = False
        if(len(userInput)<1 or len(userInput)> 9):
            isValid = False

    return isValid

main()

Ok above is what my code is so far and it does it job by correctly outputting the NATO alphabetic code and all that when letters, numbers and dashes are entered. However I am tasked with an additional stipulation on this code that I am struggling on how to go about.

Based on the user's input I am suppose to state whether the license plate tag they gave is for a "Big county", "small county, or "vanity plate". After charToWord(char) in the main function is completed.

Based on these stipulation patterns:

Big County Plate: three letters followed by three numbers. Example: ABC123:

Small County Plate: one or two digit county code from 3 to 97, a dash and then any combination of letters and numbers. Example: 93-1A2B

Vanity plate: any valid tag that's neither of the above.

Would I have to write another function for this? I believe this has something to do with string slicing. Which I am fairly new at, so I'm struggling with how to write the if statement.

Here is what I have so far:

def countyPlate(userInput):
    for currChar in userInput:
        if currChar.isalnum() and currChar == "-":
             print("vanity plate")

Am I on the right track? Any help is appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire