循环运行不正常(python 3.4)
我正在尝试做一个程序,可以告诉如果输入的车牌有效或无效。 我想让程序逐个查找输入中每个字符的索引。 我创建了三个函数,因为这个数字会弄乱索引,所以每个函数都要达到一定的长度,然后停止 我还希望程序进入循环,直到满足某个条件。当您输入不符合条件的字符串时,程序将进入循环。但是,如果您以小写形式写入一些输入字符,循环将达到收支平衡 例如,如果我写“nl03LUD”,程序会告诉我再试一次,当我写“nl03LUD”时,它不会告诉我再试一次 对不起,我的英语不好,我不知道如何解释,如果您不理解,python visualizer会让我的解释更清楚 如有任何更改和反馈,将不胜感激 这是我的节目:循环运行不正常(python 3.4),python,while-loop,boolean,python-3.4,Python,While Loop,Boolean,Python 3.4,我正在尝试做一个程序,可以告诉如果输入的车牌有效或无效。 我想让程序逐个查找输入中每个字符的索引。 我创建了三个函数,因为这个数字会弄乱索引,所以每个函数都要达到一定的长度,然后停止 我还希望程序进入循环,直到满足某个条件。当您输入不符合条件的字符串时,程序将进入循环。但是,如果您以小写形式写入一些输入字符,循环将达到收支平衡 例如,如果我写“nl03LUD”,程序会告诉我再试一次,当我写“nl03LUD”时,它不会告诉我再试一次 对不起,我的英语不好,我不知道如何解释,如果您不理解,pytho
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)
这是因为对大写字母使用ord将返回与小写字母不同的值。例如:
>>> ord('A')
>>> 65
>>> ord('a')
>>> 97
我已将您的程序修改为更具Python风格的方式:
import sys
def length_check(licenseplatenumber):
if len(licenseplatenumber) != 7:
print 'The license plate length should be 7'
return False
return True
def validate(licenseplatenumber):
for index,char in enumerate(licenseplatenumber):
if index == 0 or index == 1 :
if 65 <= ord(char) <= 90:
continue
else :
print 'Either of first two letters you have entered is NOT valid'
return False
if index == 2 or index == 3:
if 48 <= ord(char) <= 57:
continue
else:
print 'Either of third or fourth letters you have entered is NOT valid'
return False
if index == 4 or index == 5 or index == 6 :
if 65 <= ord(char) <= 90:
continue
else:
print 'Either of last three chars you have entered is NOT valid'
return False
return True
while True:
licenseplatenumber = str(input('Enter your license plate, '
'do not include space in between, enter in uppercase. \n'))
if length_check(licenseplatenumber):
if validate(licenseplatenumber):
print '%s is a valid license plate' %(licenseplatenumber)
break
这不是解决方案。此while循环一直运行,直到被“break”打断。这里,它只对有效的车牌有效。如果在遍历字符串时需要访问索引,请在enumerate'test'中为i,c使用enumerate:>>>。。。打印i,c。。。0 t 1 e 2 s 3 t您需要在输入上使用str.upper。尝试将.upper添加到输入行的末尾。