2014-10-02 42 views
-2

我想在python 3.4中创建一个程序,该程序使用列表来存储一些着名名人的名字。我想使用循环来提示用户输入名称并将它们添加到列表中。当用户输入“完成”时,循环应停止。该程序应该输出输入的名人数量。最后,程序应该使用另一个循环,以显示自己的路线的名人的名字,每一个和“做”不应该在名人名单试图在Python 3.4中创建一个程序,但没有任何运气

def main(): 
    with open("celeb1.txt", "w") as Celeb: 

    def Celebrites(): 

Celeb = ["Johnny Depp', 'Gary Busey', 'Tommy Lee Jones"]: 

#file for saving names. 


#write the list to the file 

.writelines(Celeb1.txt) 
#close file 
outfile.close() 


main() 
+0

'我正在尝试'< - 到底怎么样?您没有向我们展示您的代码 – inspectorG4dget 2014-10-02 03:01:11

+1

请修正您的帖子中的代码缩进 – inspectorG4dget 2014-10-02 03:29:29

回答

0

下面给出的是您所要求的代码。如果您可以发布您的代码,我们可以在其中查找错误。

lst = [] 
while(1): 
    name = input("enter celebrity name:") # read name of celebrity 
    if name == "done": 
     break; 
    else: 
     lst.append(name) # add name to list 
# len(list) to find length of list 
print(len(lst)) 
# print names in loop 
for item in lst: 
    print(item) 
+0

使用'list + = [name]' - 使用'list.append(name)'代替是个不错的主意。另外,你的'for'循环应该看起来像'列表中的项目:print(item)'。 – rmunn 2014-10-02 03:25:52

+0

@rmunn你能告诉我使用'list.append(name)'而不是'list + = name'的原因吗? – Joe 2014-10-02 03:31:24

+0

简短版本:因为'+ ='版本很容易出错(例如,你有它在你的评论中错了 - 它应该是'list + = [name]'而不是'list + = name')。 'list.append(name)'版本更清晰。 – rmunn 2014-10-02 03:37:43

相关问题