Search code examples
pythonfor-loopif-statementcs50

Why are my if-statements not executing in order?


so there is this coding question (from CS50's introduction to programming with python week 2, loops) where user input is reviewed by your program to check if it satisfies a set of conditions. The problem is as follows in the screenshot here is the full problem. Although not included in the conditions the input can be all text and the output should still be "valid".

so here is the program that i have written and implemented. For some reason, it seems to be picking up until the first if-statement, where it does return "Valid" when prompted with all-text input. However, once my input is no longer text only and now contains other types of input, the program stops functions, seemingly at all. I personally think the problem is with the order of if statements and their functionality relative to that, because the debug print-statements i have placed throughout the "else" statement is not executing at all. Thank you for your time,

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")


def is_valid(s):
    if 2 <= len(s) <= 6 and s.isalpha():
        return True


    else:
        if s.isalnum():
            for letter in range(len(s)):
                print(s[letter])
                return letter
            if s[letter].isdigit():
                first_number , rest = s.split(s[letter])
                print(first_number , rest)
                if int(first_number) > 0 and rest.isdigit():
                    return True
                else:
                    return False
main()

Solution

  • Here's my solution to the problem:

    def main():
        plate = input("Plate: ")
        if is_valid(plate):
            print("Valid")
        else:
            print("Invalid")
    
    
    def is_valid(s):
        if 2 <= len(s) <= 6:
            numsStarted = False;
            for i in range(len(s)):
                if (i < 2):
                    if (not s[i].isalpha()):
                        return False
                elif (numsStarted and not (s[i] >= '0' and s[i] <= '9')):
                    return False
                elif (s[i] >= '0' and s[i] <= '9'):
                    numsStarted = True;
                elif (s[i].isalpha()):
                    continue
                else:
                    return False
            return True
        else:
            return False
    main()
    

    In this code, I start the same way you did with checking that s is the correct length, then I have a for loop to take me through all the characters in s. In this loop, I check first whether the character is a number, if it is I check that all remaining characters are a number, if not, then I return False. If it's not a number, I check to see if it's a letter, and finally, if it's anything else, I just return False. If my code manages to reach the end of the loop, I can safely assume every character in s was a letter, and return True

    You had a couple of little problems in your code which caused it to not work properly. Firstly, you have a return statement in one of your if statements which caused the function to return a value prematurely. You also are missing an else statement for the if s.isalnum(): which meant that the function would sometimes return a None value.

    I hope this helps :)