In: Computer Science
A company uses codes to represent its products, for example ABC2475A5R-14. Valid codes must have:
At least 10 characters (some codes have more than 10 characters)
Positions 4 through 7 must be digits (and represents the country in which the product will be sold)
The character in the 10th position must be a capital letter and represents the security level of the product.
Write a Python program that will read 10 product codes from a text file (Codes.txt - attached below). Determine if each conforms to the rules listed above:
If it does conform, print a heading: Valid Code(s) are: and list the codes that are valid. If it does not conform, print a heading: Invalid Code(s) are: and list the codes that area invalid. Be sure to test all possible combinations.
Further, if the code is valid, due to new government security laws, products with a security level of R (restricted) are no longer to be sold in countries with a country code of 2000 or higher. Output a heading: Invalid Restricted Code(s) are: and list the codes that are invalid in case you encounter any products that violate these new laws.
here is the text file data
XYZ2755R-14
RST1234A6A-12
UVW24a6R7R-13
PQR3999F85-11
STI1281J9A-04
FOR2561T4R-54
BID2075U3R-55
AGA1475P1B01
JBT2175E5X-04
KAM1145X2R-05
Given below is the code for the question. PLEASE MAKE SURE
INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.
Codes.txt
--------
XYZ2755R-14
RST1234A6A-12
UVW24a6R7R-13
PQR3999F85-11
STI1281J9A-04
FOR2561T4R-54
BID2075U3R-55
AGA1475P1B01
JBT2175E5X-04
KAM1145X2R-05
Validate.py
-----------
def isValid(code):
if len(code) < 10:
return False
for i in range(3, 7):
if not code[i].isdigit():
return
False
if code[9].isupper():
return True
else:
return False
def isRestricted(code):
if isValid(code):
num = int(code[3:7])
if num >= 2000:
return
True
return False
def main():
filename = 'Codes.txt'
f = open(filename, 'r')
codes = []
for code in f.readlines():
code = code.strip()
codes.append(code)
f.close();
print('Valid Code(s) are:')
for i in range(len(codes)):
if isValid(codes[i]):
print(codes[i])
print()
print('Invalid Code(s) are:')
for i in range(len(codes)):
if not isValid(codes[i]):
print(codes[i])
print()
print('Invalid Restricted Code(s) are:')
for i in range(len(codes)):
if isRestricted(codes[i]):
print(codes[i])
print()
if __name__ == "__main__":
main()

