“”“”
Define the is_legitmate_code() function (Sorry about the spelling mistake - please use this exact spelling is_legitmate_code) which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a legitimate code or not. A legitimate code is a string made up of one letter followed by one or more digits (can also include spaces before or between the digits). The first three lines of code inside the function should be:
code_letters = ["A", "B", "Z", "T", "X"]
min_for_each_letter = [2, 2, 1, 0, 4] #inclusive
max_for_each_letter = [8, 9, 6, 7, 5] #inclusive
where:
code_letters is the list of code letters which are legitimate for the first letter of the code string,
min_for_each_letter is a list which contains the minimum number (inclusive) for each digit following that letter,
max_for_each_letter is a list which contains the maximum number (inclusive) for each digit following that letter.
For example, the third element of the code_letters list is the letter 'Z', the corresponding third element of the min_for_each_letter list is 1 and the corresponding third element of the min_for_each_letter list is 6. This indicates that the code digits which follows the letter 'Z' can be any number made up of the digits 1, 2, 3, 4, 5 or 6. The number part of the code string can also contain any number of spaces.
Note: The number part of a parameter code string to be tested could contain an alphabetic character thus making the code not legitimate. You will find it useful to use the method which returns True if a string is a digit, False otherwise.
For example, the following code:
print("1.", is_legitmate_code('B747346'))
print("2.", is_legitmate_code('X 444 454'))
print("3.", is_legitmate_code('T 444854'))
print("4.", is_legitmate_code('X 444X454'))
print("5.", is_legitmate_code('X '))
print("6.", is_legitmate_code('C123 '))
prints:
1. True
2. True
3. False
4. False
5. False
6. False
“”“”
def is_legitmate_code(code):
code_letters = ["A", "B", "Z", "T", "X"]
min_for_each_letter = [2, 2, 1, 0, 4]
max_for_each_letter = [8, 9, 6, 7, 5]
code_list = code.split()
code2 = ""
for element in code_list:
code2 = code2 + element
code2 = code2[1:]
new_list = list(code)
first_letter = new_list[0]
number_list = list(code2)
if first_letter in code_letters:
position = code_letters.index(first_letter)
if code2.isdigit():
for number in number_list:
if int(number) >= min_for_each_letter[position] and int(number) <= max_for_each_letter[position]:
return "True"
else:
return "False"
else:
return "False"
print("1.", is_legitmate_code('B747346'))
print("2.", is_legitmate_code('X 444 454'))
print("3.", is_legitmate_code('T 444854'))
print("4.", is_legitmate_code('X 444X454'))
print("5.", is_legitmate_code('X '))
print("6.", is_legitmate_code('C123 '))
新手求助啊,第三个总是return true,答案应该是false,找不到哪里出问题了!