python的初级编程问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 16:26:26
问题分4步,
1.Write Python program that creates a list called alphabet containing the letters of the Greek alphabet as strings ("alpha", "beta", etc.).
2.Define the functions head() and tail()
3.Define a recursive function, using head() and tail(), that prints all the items of the list in order from "alpha" to "omega" with a space between items.
4.In your program, call the recursive function with alphabet as its argument.
第一步的list是
alphabet=['Alpha','Beta','Gamma','Delta','Epsilon', 'Zeta',
'Eta', 'Theta','Iota', 'Kappa', 'Lambda','Mu',
'Nu', 'Xi','Omicron', 'Pi', 'Rho','Sigma',
'Tau','Upsilon','Phi','Chi','Psi','Omega']
请问专家后面的步骤应如何完成?

汗,前几天问过类似的题目?

#1 直接赋值,python会自动创建对象

alphabet=['Alpha','Beta','Gamma','Delta','Epsilon', 'Zeta','Eta', 'Theta','Iota', 'Kappa', 'Lambda','Mu','Nu', 'Xi','Omicron', 'Pi', 'Rho','Sigma','Tau','Upsilon','Phi','Chi','Psi','Omega']

#2 因为你没有说明head()和tail()要做说明,我就按照字面意思做了
#2 head():如果参数非空,返回参数的首个元素,否则返回None
def head(alphabet):
....if alphabet:return alphabet[0]
....else:return
def tail(alphabet):
....if alphabet:return alphabet[-1]
....else:return

#3 递归函数
def rec(a_list):
....print head(a_list)
....while head(a_list)!=tail(a_list):
........del a_list[0]
........rec(a_list)

#4 调用很简单,直接把alphabet作为rec的参数就可以:
if __name__=='__main__':
....rec(alphabet)

因为百度知道缩进处理的原