user12185419
user12185419

Reputation:

Python Validation issue

This validation will correctly mark a character invalid if it doesn't meet the function's criteria the first time. Such as if I enter $ it will kick it out and ask for new input. But then when I enter a correct input after that such as fg to get FoxTrot, Golf. It will say IndexError: list index out of range

Why is that? How can I get it to not do that?

def main():
    userInput = input("Enter license plate tag: ")
    for char in userInput:
        while(not rSeriesValidate(userInput)):
            print("TAG INVALID. Please enter a new tag.")
            userInput = input("Enter license plate tag: ")
        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 rSeriesValidate(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()

Here is the output:

Enter license plate tag: $                                                                                                                                    
TAG INVALID. Please enter a new tag.                                                                                                                          
Enter license plate tag: fg                                                                                                                                   
Traceback (most recent call last):                                                                                                                            
  File "main.py", line 40, in <module>                                                                                                                        
    main()                                                                                                                                                    
  File "main.py", line 8, in main                                                                                                                             
    charToWord(char)                                                                                                                                          
  File "main.py", line 15, in charToWord                                                                                                                      
    word = nato[charNum]                                                                                                                                      
IndexError: list index out of range  

Upvotes: 0

Views: 84

Answers (2)

Philippe Dixon
Philippe Dixon

Reputation: 87

@alexandr-shurigin hit on one issue with the lowercase letters but the other issue is scope. I think Python is not updating the userInput var in main(), instead I think it's re-declaring another userInput variable that only is scoped to the while loop.

Keep alexandr-shurigin's changes and try this for your main():

def main():
    userInput = input("Enter license plate tag: ")
    while(not rSeriesValidate(userInput)):
        print("TAG INVALID. Please enter a new tag.")
        userInput = input("Enter license plate tag: ")
    else:
        for char in userInput:
            charToWord(char)

This solves the '$' input followed by a legal input. The while-else is kind of a strange construct but legal, I'd consider separating the string validation from gathering the input. I used it for minimal change to your code.

Upvotes: 1

Alexandr Shurigin
Alexandr Shurigin

Reputation: 3981

Because you were checking against upper-cased characters but was trying with lowercased.

Fixed it for you, now it works with fg and FG as well ;)

def main():
    while True:
        userInput = input("Enter license plate tag: ")
        if not rSeriesValidate(userInput):
            print("TAG INVALID. Please enter a new tag.")
            continue

        for char in userInput:
            charToWord(char)

        break


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"]

    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 rSeriesValidate(userInput):
    isValid = True

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

    return isValid


main()

Output

python test123.py
Enter license plate tag: fGaDD
Foxtrot
Golf
Alpha
Delta
Delta

Upvotes: 0

Related Questions