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
print(charNum, char)and see whylower_case_with_underscoresstyle. 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_wordseems to be converting the input before checking whether or not it’s valid, no?