2017-04-30 62 views
1

我如何解释这个...重复功能追加到列表

我是新来的Python和目前使用最新版本的Python 2中

我写(或试图写入)一记录制作并将制作添加到列表的程序。我的目标是反复提示用户添加作品,直到他们选择“否”,此后程序应该转到下一个阶段。

到目前为止,我已经定义,询问用户是否想记录生产,然后要求生产名称,描述和结果的功能。然后用用户的输入更新相应的列表。

我的失败就是我不能让程序然后询问用户是否愿意添加其它生产,然后关闭循环,如果他们选择“否”。

这里是我的代码:

locus = raw_input('Enter location where statement compiled: ') 
colleague = raw_input('Enter name of corroborating officer: ') 
productions = [] 
descriptions = [] 
result = [] 

def logger(): 
    log = raw_input('Would you like to log a production? Y or N: ') 
    if log == 'Y' or 'y': 
     new_production = raw_input('ENTER NAME OF PRODUCTION: ') 
     productions.append(new_production) 
     new_description = raw_input('ENTER DESCRIPTION OF PRODUCTION: ') 
     descriptions.append(new_description) 
     new_result = raw_input('ENTER SUMMARY OF CONTENTS FOUND: ') 
     result.append(new_result) 
     print new_production 
     print new_description 
     print new_result  
     return True 
    else: 
     return False 

logger() 

while True: 
    finished = raw_input('Do you want to submit another? Y or N: ') 
    if finished == 'Y' or 'y': 
     logger() 
    else: 
     return False 

预先感谢您可以提供任何帮助。

回答

0

你可以尝试这样的事:

while True: 
    inp = raw_input('Would you like to enter a new log? Y or N: ') 
    if inp.lower() == "y": 
     # do your required functions to create a log in here 
     # or you could call logger(), as long as you modify it appropriately 
     new_production = raw_input('ENTER NAME OF PRODUCTION: ') 
     ... 
     print new_description 
     print new_result  
    elif inp.lower() == "n": 
     break 
    else: 
     print "invalid response" 

让我知道,如果你想为它的任何解释!

+0

这似乎已经非常完美,是正是我一直在后 - 你能不能给我反馈,我要去哪里错或其他任何建议我重新编码上面?谢谢。 –

+0

你写循环的方式并不差,只是可能不够简洁。另外,当试图停止运行一个循环时,你应该'打破'。以前,它看起来像你正在做'返回假' - 但只有函数可以返回值。这也对循环没有影响。否则,其余的代码非常好!很高兴我能提供帮助,如果这是你正在寻找的东西,请把答案标记为正确(绿色勾号)! – Windmill