-1

我正在尝试创建一个程序,如果用户想要将名称添加到列表中,它将询问一个问题,从列表中编辑名称删除名称或显示列表。我得到了一点,我在请求完成后重新出现问题时遇到问题。因此,在按下“1”并完成操作后,我想再次提问,直到用户退出程序。

这就是我所拥有的

#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()
4

4 回答 4

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]")
于 2013-04-25T20:15:16.637 回答
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
于 2013-04-25T20:20:51.297 回答
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)
于 2013-04-25T20:21:49.463 回答
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 
于 2013-04-25T20:26:19.287 回答