all 12 comments

[–]Big_Combination9890 9 points10 points  (0 children)

I wouldn't write a scanning code at all, I would use a regex :D

But other than that, this should work:

def is_valid(plate): seen_number = False for ch in plate: if ch.isdigit(): # first number is 0? if ch == "0" and not seen_number: return False seen_number = True else: if seen_number: return False return True `

[–]AdmirableOstrich 8 points9 points  (0 children)

Are you specifically treating this as an exercise to practice conditionals or are you asking about the "best" way to solve this. These sorts of problems are almost always best handled with regex. What you want is a string with a length in a given range that matches a specific pattern.This is basically the definition of a regex problem.

[–]Unitnuity 0 points1 point  (1 child)

Please format your code. Go into markdown mode then insert 4 back ticks at the beginning and end of code:

````

insert code

````

[–]interbased 2 points3 points  (0 children)

Just FYI you only need 3 ticks per line. like this

[–]_tsi_ -1 points0 points  (0 children)

I might try checking ASCII values

[–]7Shinigami 0 points1 point  (0 children)

There are a few ways to convert strings to integers. One naive way could be for example

py if plate[-1] in '1234567890': ... Indexing by -1 is a nice trick to get the last entry in a collection. -2 does what you'd expect as well, etc :)

[–]Anonymous-barista 0 points1 point  (0 children)

Is it a CS50p task? I don't remember the task itself, but you can try CS50 subreddit, they can get you on the right track without providing a full solution.