Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何退出while循环_Python_While Loop - Fatal编程技术网

Python 如何退出while循环

Python 如何退出while循环,python,while-loop,Python,While Loop,目前我正在做我的作业。要求是测试学生ID的格式。我想知道为什么我的while循环不能正常工作。。 我的验证检查如下: def isValidStudentIDFormat(stid): # studentID must have a length of 9 if(len(stid) != 9): # return the invalid reason return "Length of Student ID is not 9" # studentID must starts

目前我正在做我的作业。要求是测试学生ID的格式。我想知道为什么我的while循环不能正常工作。。 我的验证检查如下:

def isValidStudentIDFormat(stid):

# studentID must have a length of 9
if(len(stid) != 9):
    # return the invalid reason
    return "Length of Student ID is not 9"

# studentID must starts with a letter S
if(stid[0] != 'S'):
    # return the invalid reason
    return "First letter is not S"

# studentID must contains 7 numbers between the two letters
for i in range(1,8):
    # anything smaller than 0 or bigger than 9 would not be valid.
    # so does a character, will also be invalid
    if((stid[i] < '0') or (stid[i] > '9')):
        # return the invalid reason
        return "Not a number between letters"

if((stid[8] < 'A') or (stid[8] > 'Z')):
    # return the invalid reason
    return "Last letter not a characer"

# return True if the format is right
return True

然而,即使我输入了正确的学生ID格式,我也会得到一个无限循环。有人能帮我吗?

你应该在分配时比较
结果==True
。但是,您不会检查新学生id的有效性,这可以通过以下方式完成:

while (result != True):
    print("Invalid student id format. Please check and enter again.")
    stid = input("Enter student id: ")
    result = isValidStudentIDFormat(stid)
?


缩进指示python内部的流程-请按照IDE中的格式设置代码的格式。代码缩进是否与上面的格式完全相同?如果没有,请编辑以上内容,使其看起来像是在您的编辑器上。与其他语言不同,缩进在python中很重要,通常会影响代码的解释方式,返回字符串和布尔值只会导致混淆。你应该了解
try
raise
如果你想要异常消息,伙计们,我的缩进是正确的,我不知道复制时发生了什么。我会改的。嗨,伙计们,我不知道如何使缩进正确。。但你们可以假设我的缩进是正确的。。哈哈
while (result != True):
    print("Invalid student id format. Please check and enter again.")
    stid = input("Enter student id: ")
    result = isValidStudentIDFormat(stid)
def validateStudentIDFormat(stid):
    if len(stid) != 9:
        raise RuntimeError("Length of Student ID is not 9")

    if stid[0] != 'S':
        raise RuntimeError("First letter is not S")

    for char in stid[1:-1]:
        if char.isnumeric(): 
            raise RuntimeError("Not a number between letters")

    if not stid[-1].isalpha():
        raise RuntimeError("Last letter not a characer")
while True:
    stid = input("Enter student id: ")
    try:
        validateStudentIDFormat(stid) 
    except RuntimeError as ex:
        print(ex)
        print("Invalid student id format. Please check and enter again.")
    else:
        break