主要功能已编写,但您需要填写其他功能 功能体,以便:
第一个函数采用参数n,该参数表示前n个项 的几何序列:{1,4,16,64,256,…},加上以下各项, 并返回条款的总数。输入5的示例 会产生:从1到256的几何级数之和是341
第二个函数采用参数n,它表示算术序列的前n个项:{1 / 3,2 / 3,3/3,4/3,…},将 项,并返回要在输出中显示的总数。
第三个函数接受一个单词,如果单词的字母顺序相反,则返回True;否则返回False;否则,返回False。反向的例子 字母顺序的单词是:韩元,pi,角色,吨。
def main():
num = int(input('Enter a number of terms: '))
total = ex4a(num)
print("The sum of the geometric series from 1 to {} is {}".\
format(4 ** (num - 1), total))
total = ex4b(num)
print("The sum of the arithmetic series from 1/3 to {}/3 is {:.5f}".format(num, total))
word = input("Enter a word: ")
isReverse = ex4c(word)
negation = 'not '
if isReverse:
negation = ''
print("The word {} is {}in reverse alphabetical order".format(word, negation))
def ex4a(num):
s = 0
i = 1
for el in range(num):
s += i
i = 4 * i
return s
def ex4b(num):
s = 0
for i in range(num):
s += i / 3
return s
def ex4c(word):
for i in range(len(word) - 1):
if word[i] > word[i + 1]:
return True
else:
return False
答案 0 :(得分:0)
错误,如果每个有效i的word [i]> word [i + 1],则函数返回True, 换句话说,如果有任何单词[i] <=单词[i + 1]
,则返回Falsedef ex4c(word):
for i in range(len(word) - 1):
if word[i] <= word[i + 1]:
return False
return True