Jul-22-2019, 11:46 AM
I need to write a program that has these conditions:
- Length must be exactly 9
- The first letter must be S, T, F or G
- Must consist of 7 digits
- Reference letter must be A to Z or a to z
Expected Output:
My Code:
- Length must be exactly 9
- The first letter must be S, T, F or G
- Must consist of 7 digits
- Reference letter must be A to Z or a to z
Expected Output:
Output:Enter String: S1234567A
Valid String
Enter String: X123456789B
Error, Length must be exactly 9
Enter String: S12345XXB
Error, Must consist of 7 digits
The requirement is to make use of isdigit() and isalpha() function but I read up on various resources online it seems it's most commonly used to return either True/False for the value.My Code:
def main(): enterstr = input("Enter String: ") firstletter = ["S", "T", "F", "G"] refletter = ['A', 'B', 'C', 'D'] if enterstr.isdigit() < 7: print("Error, Must consist of 7 digits") elif enterstr.isalpha() [0] != firstletter: print("Error, The first letter must be S, T, F or G") elif enterstr.isalpha() [-1] != refletter: print("Error, Reference letter must be A to Z or a to z") elif enterstr.count < 9: print("Error, Length must be exactly 9") else: print("Valid String") main()I keep getting the output "Error, Must consist of 7 digits" no matter how many characters the String is. Need your advise, thanks.