Nov-16-2018, 08:33 AM
Hi guys, currently I am doing my assignment. The requirement is to test the format of Student ID. I wonder why is my while loop is not working properly..
My code is as below:
My code is as below:
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 def insert_student_record(): #printing the message to ask user to insert a new student into the memory print("Insert a new student \n") fn = input("Enter first name: ") #check if user entered space #strip() returns a copy of the string based on the string argument passed while not fn.strip(): print("Empty input, please enter again") fn = input("Enter first name: ") ln = input("Enter last name: ") while not ln.strip(): print("Empty input, please enter again") ln = input("Enter last name: ") stid = input("Enter student id: ") while not stid.strip(): print("Empty input, please enter again") stid = input("Enter student id: ") result = isValidStudentIDFormat(stid) while (result != True): print("Invalid student id format. Please check and enter again.") stid = input("Enter student id: ") #append the student details to each list #append first name fName.append(fn) #append last name lName.append(ln) #append student id sid.append(stid) #to check if the new student is in the lists if stid in sid: if fn in fName: if ln in lName: #then print message to tell user the student record is inserted print("A new student record is inserted.")Of course the code is only part of my assignment. The rest of code works well. My problem is that below part when I am checking for ID format, I'm getting an infinite loop.
result = isValidStudentIDFormat(stid) while (result != True): print("Invalid student id format. Please check and enter again.") stid = input("Enter student id: ")I couldn't figure out which part is wrong. Can help me? Thanks!