循环不能正常工作(python 3.4)

时间:2015-03-13 23:58:21

标签: python while-loop boolean python-3.4

我正在尝试制作一个可以判断输入牌照是否有效的程序。 我希望程序逐个查找输入的输入中每个字符的索引。 我创建了三个函数,因为这个数字会使索引变得混乱,因此每个函数都会达到一定的长度然后停止。

我还希望程序进入循环直到满足某个条件。当您输入不符合条件的字符串时,程序会进入循环。但是,即使用小写字母写下一些输入字符,循环也会中断。

例如,如果我写'nl03LUD'程序告诉我再试一次,当我写'NL03lud'时它不会告诉我再试一次。

抱歉我的英文不好,我不知道如何解释,如果你不理解,python visualizer会让我的解释更清晰。

非常感谢任何改动和反馈

这是我的计划:

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)

2 个答案:

答案 0 :(得分:1)

这是因为在大写字母上使用ord将返回与小写字母不同的值。例如:

>>> ord('A')
>>> 65
>>> ord('a')
>>> 97

答案 1 :(得分:-1)

我已经将您的程序修改为更加pythonic的方式:

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