Python Forum
While loop to run a boolean test
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop to run a boolean test
#1
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:
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!
Reply
#2
two things:
1. you never change result within the loop
2. another problem that will show up once you fix 1. is that your function isValidStudentIDFormat returns strings, so result will be str and will never become equal to True, i.e. (bool)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi how can I change then?
Reply
#4
(Nov-16-2018, 09:39 AM)vicson Wrote: Hi how can I change then?
Sorry, item 2 in my list is not correct, please ignore it. I didn't see that you have return True at the end of your function.
As to item 1 - just add a line, in the while body, that changes result after user input
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Nov-16-2018, 09:49 AM)buran Wrote:
(Nov-16-2018, 09:39 AM)vicson Wrote: Hi how can I change then?
Sorry, item 2 in my list is not correct, please ignore it. I didn't see that you have return True at the end of your function.
As to item 1 - just add a line, in the while body, that changes result after user input

Hi, I have added a line that changes the result. However I still face the same problem can't exit the while loop..

    result = isValidStudentIDFormat(stid) 
    while (result != True):
        print("Invalid student id format. Please check and enter again.")
        stid = input("Enter student id: ")
        result == True
Reply
#6
if you did this, you will exit the this while loop for sure.
However you don't want to set result to True, you want to use isValidStudentIDFormat function.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Nov-16-2018, 09:54 AM)vicson Wrote:
(Nov-16-2018, 09:49 AM)buran Wrote: Sorry, item 2 in my list is not correct, please ignore it. I didn't see that you have return True at the end of your function.
As to item 1 - just add a line, in the while body, that changes result after user input

Hi, I have added a line that changes the result. However I still face the same problem can't exit the while loop..

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

sorry I don't get it. Isn't result a variable for isValidStudentIDFormat? Aren't they the same thing?
sorry I am a newbie to programming...
Reply
#8
you want to check user input every time when you take it.
you take user input once, you check it using the function and assign what it return to result variable. If it is not True (input is invalid) you enter a while loop and then you need to check user input and assign that to result variable and repet until it is valid input
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020