Python 编程请教

来源:百度知道 编辑:UC知道 时间:2024/05/16 03:25:53
1.Use a for-loop to print all the items of the list in order from "alpha" to "omega" with a space between items.

2.Use a while-loop to print the items in order from "alpha" to "lambda" with a space between items. The loop should test each item for non-equality with "lambda".

下面是一段代码
for alphabet in ['Alpha','Beta','Gamma','Delta','Epsilon', 'Zeta',
'Eta', 'Theta','Iota', 'Kappa', 'Lambda','Mu',
'Nu', 'Xi','Omicron', 'Pi', 'Rho','Sigma',
'Tau','Upsilon','Phi','Chi','Psi','Omega']:
print alphabet
请教如何用for或while将alphabet打印在同一行,并且每两个元素之间有空格?

很简单
print "str",
注意后面那个逗号

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

# for method
for alphabet in alphabetlist:print alphabet,

# while method
i=0
while 1:
print alphabetlist[i],
if alphabetlist[i]!="lambda":i=i+1
else:break