关于python中的函数问题
我想写两个函数,并且通过下面的测试,请大家帮帮忙问题一:编写一个函数 one_word(words),它接收字符串列表并使用 for... in... 循环将所有单词连接到一个字符串中(即“correcthorsebatterystaple”)。
测试:
words = ["correct", "horse", "battery", "staple"]
assert(one_word(words)=="correcthorsebatterystaple")
问题二:
编写一个函数 as_and_es(words),它使用循环计算 2𝑎+𝑒,其中 𝑎 是字母“a”在所有单词中出现的次数,而 𝑒 是字母“e”出现的次数。
测试:
words = ["correct", "horse", "battery", "staple"]
assert(as_and_es(words)==8)
words = ["correct", "horse", "battery", "staple"]
def one_word(words):
a = ''
for i in words:
a += i
return a
def as_and_es(words):
b = one_word(words)
x = 0
y = 0
for j in b:
if j == 'a':
x += 1
elif j == 'e':
y += 1
sum = 2 * x + y
return sum
print(one_word(words))
print(as_and_es(words)) 重发帖吧,3个鱼币还不如不悬赏
而且这是个大活 def one_word(words):
return "".join(words)
def as_and_es(words):
word = "".join(words)
return 2 * word.count("a") + word.count("e")
喔
页:
[1]