【发布时间】:2015-05-16 14:53:22
【问题描述】:
我正在尝试制作一个程序来判断输入的车牌是否有效。 我希望程序在输入的输入中一一找到每个字符的索引。 我创建了三个函数,因为数字会弄乱索引,所以每个函数都打算达到一定的长度然后停止。
我还希望程序进入一个循环,直到满足某个条件。当您输入一个不满足条件的字符串时,程序确实进入循环。但是,即使您将某些输入字符写成小写,循环也会中断。
例如,如果我写 'nl03LUD' 程序告诉我再试一次,当我写 'NL03lud' 时它不会告诉我再试一次。
对不起,我的英语不好,我不知道如何解释,如果你不明白,python可视化器会让我的解释更清楚。
任何修改和反馈将不胜感激
这是我的程序:
import sys
def lengthchecker(licenseplate):
length = len(licenseplate)
if length == 7:
return('Length of license plate is valid')
while length is not 7:
sys.exit('Length of license plate not valid')
licenseplate = str(input('Enter your license plate, do not include space in between, enter in uppercase. \n'))
print(lengthchecker(licenseplate))
def checkletter1(licenseplate):
x = True
a = 0
while 0 <= a <= 1:
letter = licenseplate[a]
index = ord(letter)
if 65 <= index <= 90:
a = (a + 1)
else:
x = False
return x
return x
def checkletter2(licenseplate):
y = True
b = 2
while 2 <= b <= 3:
letter1 = licenseplate[b]
index1 = ord(letter1)
if 48 <= index1 <= 57:
b = (b + 1)
else:
y = False
return y
return y
def checkletter3(licenseplate):
z = True
c = 4
while 4 <= c <=6:
letter2 = licenseplate[c]
index2 = ord(letter2)
if 65 <= index2 <= 90:
c = (c + 1)
else:
z = False
return z
return z
x = checkletter1(licenseplate)
if x is True:
print('The first two letters you have entered is valid')
while x is False:
licenseplate = str(input('Enter your license plate again \n'))
x = checkletter1(licenseplate)
y = checkletter2(licenseplate)
if y is True:
print('The third to fifth letters you have entered is valid')
while y is False:
licenseplate = str(input('Enter your license plate again \n'))
y = checkletter2(licenseplate)
z = checkletter3(licenseplate)
if z is True:
print('The last three letters you have entered is valid')
while z is False:
licenseplate = str(input('Enter your license plate again \n'))
z = checkletter3(licenseplate)
【问题讨论】:
标签: python while-loop boolean python-3.4