I’m new to coding and python in general and I’m taking a course to start to expand my resume of sorts. I’m at the stage where we are just getting into loops and conditionals.
The prompt is to write a program that can validate or invalidate a text string based on certain conditions of the string.
The conditions are as follows:
- “All strings must start with at least two letters.”
- “… strings may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.”
- “Numbers cannot be used in the middle of strings they must come at the end. For example, AAA222 would be an acceptable string; AAA22A would not be acceptable. The first number used cannot be a ‘0’.”
- “No periods, spaces, or punctuation marks are allowed.”
the bones for this problem is:
def main(): plate = input("string: ") if is_valid(string): print("Valid") else: print("Invalid") def is_valid(s): ... main() the problem I'm having is that I'm not sure if I'm misunderstanding the question or I'm overcomplicating it. conditions 1,2 and 4 seem easy enough, it is mainly condition 3 I'm really struggling with
how to write the code to parse if the numbers are only in the latter half of the string and if the number part starts with a 0. using essentially only intro-level Python code. I could be missing something very obvious.
I am just sort of brute forcing I wrote a function to see if I could see any avenues or ways through, yet I'm stuck, I'm just trying to write a function to evaluate condition 3 and return true or false.
def is_num(string): i=0 while i < (len(string)): for c in string: #print(c, end="") if c.isalpha() == False: if c == "0": return False else: i+=1 else: i+=1 but then again, I'm very new and very lost.
Источник: https://stackoverflow.com/questions/781 ... heck-logic