1 新手python出错找不到位置哇!求帮助

“”“”
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,找不到哪里出问题了!



请先 登录 后评论

4 个回答

乔衣
...
             for number in number_list:
                if int(number) >= min_for_each_letter[position] and int(number) <= max_for_each_letter[position]:
                    return "True"
...

这一段代码,第一次进这个循环:
position: 3
number: 4min_for_each_letter[position]: 0
max_for_each_letter[position]: 7

这个表达式为True:
if int(number) >= min_for_each_letter[position] and int(number) <= max_for_each_letter[position]

所以条件成立,返回结果永远是True...
请先 登录 后评论
咪子喵子

谢谢!我用了个笨办法,改成这样算是勉强通过了,但是感觉很啰嗦,请问我怎么改那一部分比较好呢?

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)
    total = 0
    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]:
                    total = total + 1
                    if total == len(number_list):
                        return "True"
                else:
                    return "False"
                    
          else:
              return "False"
    else:
        return "False"
请先 登录 后评论
千四儿
如果直接取数字的最大值和最小值会怎么样呢?
int(max(number_list)), int(min(number_list)) 然后再进去判断能否简单一些?
请先 登录 后评论
安诺斯诺
  1. def is_legitmate_code(code: str) -> bool:
  2.     code = [str(i) for i in code if i != ' ']
  3.     code_letters = ["A", "B", "Z", "T", "X"]
  4.     if code[0] not in code_letters or len(code) <= 1:
  5.         return False
  6.     try:
  7.         temp = code[1:]
  8.         n = [int(i) for i in temp]
  9.     except:
  10.         return False     
  11.     min_for_each_letter = [2, 2, 1, 0, 4] # inclusive
  12.     max_for_each_letter = [8, 9, 6, 7, 5] # inclusive
  13.     idx = code_letters.index(code[0])
  14.     nums = [i for i in range(min_for_each_letter[idx], max_for_each_letter[idx]+1)]
  15.     for i in range(1, len(code)):
  16.         if int(code[i]) not in nums:
  17.             return False
  18.     return True
复制代码
请先 登录 后评论
  • 4 关注
  • 0 收藏,96 浏览
  • 帅哥淦 提出于 2021-09-23 22:23

相似问题