TheSudoCat
TheSudoCat

Reputation: 23

How to make sure first number in string isn't a 0?

This is for Harvard Uni's CS50P, the requirements given were that I should implement a function is_valid that checks if the user input fits the requirements for a vanity plate.

My code fulfills all requirements except "The first number used cannot be a ‘0’."

The function is_valid needs an if/else statement that takes the user input (string) (passed to is_valid as "s") and makes sure that the first number in it isn't a 0.

Ideally it would return False if the first number found is a 0 since that would make the plate invalid as a vanity plate.

The Requirements are listed here: https://cs50.harvard.edu/python/2022/psets/2/plates/

My code is the following: (Comments removed since I don't get Github formatting)

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

def is_valid(s):
    if len(s) < 2 or len(s) > 6: 
        return False

    if s[0].isdigit() == True or s[1].isdigit() == True: 
        return False

    for i in range(len(s)): #
        if s[i].isdigit():
            if not s[i:].isdigit():
                return False

    if s.isalnum() == False:
        return False

    else:
        return True
main()

Upvotes: 1

Views: 2219

Answers (4)

FelixOakz
FelixOakz

Reputation: 19

You should just plug into your first digit validation if the digit equals zero:

if s[i].isdigit() and s[i] == '0':

that should be enough! :)

Upvotes: 0

mosa
mosa

Reputation: 1

Below is my complete solution to the problem. I used a variable (flag) to track the start of digits in the string, changed the flag from 0 (digit encountered) to 1 (start of digits) and checked whether the first digit encountered was 0.

Hope this helps.

def main():
    plate = input('Enter your choice : ')
    if is_valid(plate):
        print('Valid')
    else:
        print('Invalid')

def is_valid(text):
    if len(text) >=2 and len(text) <=6 and text.isalnum() and text[0:2].isalpha():  
        flag=0      #means no digit has been encountered yet
        for c in text:
            if c.isnumeric():
                if c=='0' and flag==0:    # checks if the first digit encountered is 0
                    return False
                flag=1    # if the first digit is not 0, change flag to 1 to denote start of digits
            else:
                if flag ==1:     #checking if an alphabet comes after a digit
                    return False
        return True
    else:
        return False 

main() 

Upvotes: 0

Jred0n29
Jred0n29

Reputation: 116

A possible complete solution to all the requirements of the program would be:

def main():
  plate = input("Plate: ")
  if is_valid(plate):
    print("Valid")
  else:
    print("Invalid")
def is_valid(s):
  #We verify that the first number is not zero and the others are alphanumeric 
  for i in range(len(s)):
    if s[i].isdigit() :  
      if int(s[i]) == 0 : 
        return False
      elif not(s[i+1::].isalpha()) == False: #Check if the following characters are digits
        return False
  #----------------------------------------------------------------
  if len(s) < 2 or len(s) > 6:
    return False
  elif s[0].isdigit() or s[1].isdigit(): #We optimize the logical operators
    return False
  elif s.isalnum() == False:
    return False
  else:
      return True
main()

Upvotes: 0

Yusuf Syam
Yusuf Syam

Reputation: 771

First you have to loop over the string, and if current char is a digit, then if it is 0, return False, otherwise, go to the next step


for i in s:
    if i.isdigit():
        if int(i)==0:
            return False
        else:
            break

Upvotes: 1

Related Questions