Код: Выделить всё
def FormatCheck(choice):
while True:
valid = True
userInput = input(choice).upper()
firstPart = userInput[:2]
secondPart = userInput[2:4]
thirdPart = userInput[4:]
firstBool = False
secondBool = False
thirdBool = False
if firstPart.isalpha() == True:
firstBool = True
elif secondPart.isdigit() == True:
secondBool = True
elif thirdPart.isalpha() == True:
thirdBool = True
else:
print ("Your registration plate is private or is incorrect")
firstBool = False
secondBool = False
thirdBool = False
if firstBool == True and secondBool == True and thirdBool == True:
return userInput
break
choice = FormatCheck("Please enter your registration plate")
print(choice)
Во-первых, есть ли способ чтобы создать своего рода логический список, добавьте в него результат каждой проверки формата, а затем, если какой-либо из результатов окажется ложным, заставьте пользователя снова ввести регистрационный знак. Это устранит необходимость в длинных операторах if и избыточных переменных.
Во-вторых, могу ли я что-нибудь сделать в цикле while, чтобы проверить три раздела вместо использования три оператора if?
Заранее спасибо
Подробнее здесь: https://stackoverflow.com/questions/471 ... ion-plates