2013-04-25 124 views
-1

我正在嘗試創建一個程序,用於詢問用戶是否想向列表中添加名稱,從列表中刪除名稱或顯示列表中的名稱。我收到了一點點的問題,我有一個問題在請求完成後重新出現問題。所以在「1」被按下並且動作完成之後,我想再次出現問題直到用戶退出程序。Python名稱項目

這裏是我

#Creating a Searching? 

names = [] 
answer = input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry   [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 

# IF creating 

if answer == "1" : 
    # collect information 

    firstname = input("What is the persons first name? ") 

    #add data 

    names.append(firstname) 
    answer = input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 

# Display Data 

elif answer == "2" : 

print(names) 
answer = input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 


# USER Quit 

elif answer == "5": 
    exit() 
+2

爲什麼不使用while循環? – 2013-04-25 20:12:33

+0

@Bartlomiej Lewandowski我真的很新的蟒蛇我怎麼會這樣做 – zachstarnes 2013-04-25 20:13:56

+1

http://docs.python.org/2/reference/compound_stmts.html#while – cmd 2013-04-25 20:15:30

回答

1

我想,你需要一個while循環:

while True: 
    answer = input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 
    # collect information 
    if answer == "1": 
     firstname = input("What is the persons first name? ") 
     names.append(firstname) # add data 
    # Display Data 
    elif answer == "2": 
     print(names) 
    # USER Quit 
    elif answer == "5": 
     exit() # Or just break 
1

使用循環。

while True: 

    answer = raw_input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry   [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 

[Keep the rest as you had it before] 

    # USER Quit 

    elif answer == "5": 
     break 

,並刪除第二(和第三):

answer = input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry   [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 
+0

該程序不會顯示所有問題現在由於某種原因 – zachstarnes 2013-04-25 20:17:24

+0

我更新了我的答案,這是否更好? – dave 2013-04-25 20:26:47

+0

你需要在這裏使用'raw_input'而不是'input' – 2013-04-25 20:27:27

0
name_list = [] 
help_msg = """Create An Entry [Press 1] \n 
Display List [Press 2] 
Edit an Entry [Press 3] 
Remove Entry [Press 4] 
Quit [Press 5]: """ 

while True: 
    answer = raw_input(help_msg) 
    if answer is not '5': 
     if answer is '2': 
      print "Current list: " + ', '.join(name_list) 
      continue 
     name_list.append(answer) 
    else: 
     break 

print "Final list: " + ', '.join(name_list) 
0

你有正確的想法...只需使用一個while循環。您還必須將您所有的input()材料更改爲raw_input()。以下是您所寫的所有內容,因此無需修改代碼即可使其更好。你是新的,你應該自己去學習,但這樣你至少可以運行你的代碼:

names = [] 
inputting = True 
while inputting: 
    answer = raw_input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry   [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 

    # IF creating 

    if answer == "1" : 
     # collect information 

     firstname = raw_input("What is the persons first name? ") 

     #add data 

     names.append(firstname) 
    elif answer == "2" : 
     print(names) 
     answer = raw_input("Create An Entry [Press 1] \nDisplay List [Press 2] \nEdit an Entry [Press 3] \nRemove Entry [Press 4] \nQuit [Press 5]") 


    # USER Quit 

    elif answer == "5": 
     inputting = False