-3

有 6 个测试用例,其中 5 个基于 python 3 在字符串操作问题上通过,但 1 个测试用例自开始以来就失败了。请帮帮我。问题如下:一个函数中给出了8个字符串。

  1. 从字符串的两端删除空格:first、second、parent、city
  2. 大写 : first, second, parent
  3. 打印带空格的字符串:first、second、parent、city
  4. 检查字符串:“电话”是否仅包含数字
  5. 检查电话号码是否以字符串“start”中的值开头并打印结果(真或假)
  6. 打印:总数 'strfind' 出现在字符串中的次数:第一、第二、父母、城市
  7. 打印:在 'string1' 上使用 split 函数生成的列表
  8. 在“城市”中查找“strfind”的位置

我的代码如下:让我知道我做错了什么。5/6 个测试用例通过,只有 1 个测试用例因未知原因失败。:(

def resume(first, second, parent, city, phone, start, strfind, string1):

    first = first.strip()
    second = second.strip()
    parent = parent.strip()
    city = city.strip()
    first = first.capitalize()
    second = second.capitalize()
    parent = parent.capitalize()
    print(first + " " + second + " " + parent + " " +city)
    print(phone.isdigit())
    print(phone[0]==start[0])
    res = first + second + parent + city
    res_count = res.count(strfind)
    print(res_count)
    print(string1.split())
    print(city.find(strfind))
4

2 回答 2

0
first = first.strip()
second = second.strip()
parent = parent.strip()
city = city.strip()
first = first.capitalize()
second = second.capitalize()
parent = parent.capitalize()
print(first + " " + second + " " + parent + " " +city)
print(phone.isnumeric())
print(phone.startswith(start))
res = first + second + parent + city
res_count = res.count(strfind)
print(res_count)
print(string1.split())
print(city.find(strfind))
于 2021-03-28T19:31:43.480 回答
0

如果没有给出测试用例的详细信息,就不太确定。但是,数字 5 可能不正确,因为您只检查字符串的第一个值是否相同。这与检查“电话号码是否以字符串'start'中的值开头”不同。我建议改用以下代码:

print(phone.startswith(start))

此外,数字 6 似乎可能会导致重叠字符串出现一些不匹配。相反,我建议使用:

print(first.count(strfind) + second.count(strfind) + parent.count(strfind) + city.count(strfind))
于 2021-03-24T18:37:54.883 回答