0

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  
4
  • 1
    Please edit your question to include the full error traceback, as that can help figure out where and why the error is occurring Commented Dec 11, 2019 at 21:29
  • add print(charNum, char) and see why Commented Dec 11, 2019 at 21:32
  • Variable and function names should follow the lower_case_with_underscores style. The parentheses surrounding the condition in a for/while loop are unnecessary, this isn’t Java ;) It looks like there are some design issues, too. char_to_word seems to be converting the input before checking whether or not it’s valid, no? Commented Dec 12, 2019 at 2:27
  • If you could elaborate just a touch on what your program is meant to do, I should be able to produce a fully refactored version tonight. Commented Dec 12, 2019 at 2:34

2 Answers 2

1

@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.

Sign up to request clarification or add additional context in comments.

1 Comment

yep this is what it is! Thank you!
0

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

6 Comments

Sorry Alexander it is still doing it. Look at my edit with output now. I think it has something to do with my main.
@JustinSwartz have you tried my source code? Because I've added a few patches there
Sorry I'm new to python and don't know what that means. When you enter a invalid character like $. Then enter a valid one like f. It comes up a index error. I'm trying to figure out to fix that.
Just create a new file with my source code provided in the answer and run it. It works as expected, after that you can compare your and my codes :)
yeah sorry it still doesn't work for me. Try putting the $ in first and when it asks to reenter try putting in the letter f. I get an error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.