在将名称添加到列表和其他错误之后,我将如何停止循环

How would I stop the loop when after its added the names to the list and other errors

在添加用户输入的名称数量后,我如何停止我的代码循环而不是这样做:

  1. 添加名称
  2. 显示列表
  3. 退出

输入您的选择:1

您想输入多少个名字:2 # 我如何在这里设置最多 10 个名字?

输入姓名:鲍勃

输入姓名:吉姆

您想输入多少个名字:# 我如何阻止这一行重复?

实际代码:

names = []
def displayMenu():
    print(" 1. Add Name")
    print(" 2. Show list")
    print(" 3. Quit")
    choice = int(input("Enter your choice : "))
    while choice >5 or choice <1:
        choice = input("Invalid. Re-enter your choice: ")
    return choice

def addname():
    while True:
        number=int(input('How many names would you like to enter: '))
        name = [input('Enter name:') for _ in range(number)]
        names.append(name)

def displayData():
    #name.split(",") how would i correctly user split here
    print(names) 

option = displayMenu()

while option != 3:
    if option == 1:
       addname()
    elif option == 2:
        displayData()
    option = displayMenu()

print("Program terminating") 

而不是while True,你需要使用while i < number,所以你的addname()函数应该如下:

def addname():
    i = 0
    number = int(input('How many names would you like to enter: '))
    while i < number:
        name = input('Enter name: ')  # We ask user to enter names one by one
        names.append(name)
        i += 1

在您的情况下,for 循环也可以工作:

def addname():
    number = int(input('How many names would you like to enter: '))
    for i in range(number):
        name = input('Enter name: ')
        names.append(name)

好的,首先,因为你只有三个菜单选项,这一行:

while choice >5 or choice <1:

应该是这样的:

while 3 < choice < 1:

因此您的 displayMenu 函数如下所示:

names = []
def displayMenu():
    print(" 1. Add Name")
    print(" 2. Show list")
    print(" 3. Quit")
    choice = int(input("Enter your choice : "))
    while 3 < choice < 1: # Only accept choices in between 3 and 1
        choice = input("Invalid. Re-enter your choice: ")
    return choice

您还说过您的 addname 函数一直在循环,这是因为您有一个无限的 while 循环。

正如@ettanany 所说,您需要的是一个 for 循环:

In your case, for loop would work also:

def addname():
    number = int(input('How many names would you like to enter: '))
    for i in range(number):
        name = input('Enter name: ')
        names.append(name)

它的作用是询问用户他想输入多少个名字,然后在循环内运行代码多次——因此如果用户输入数字 9,它将要求输入 9 个名字.

你也说了最多10个名字。我们可以像在 displayMenu 函数中那样使用 while 循环来确保用户输入的数字是 10 或以下:

def addname():
    number = int(input('How many names would you like to enter: '))
    while number > 10: # Don't allow any numbers under 10
        number = int(input('Please enter a number under 10: '))
    for i in range(number):
        name = input('Enter name: ')
        names.append(name)

最后,在您的 displayData 函数中,您想要 'split' 名称并将它们打印出来。

只要做 print(names) 就会得到这样的结果:

[ 'Spam', 'Eggs', 'Waheed' ]

如果我们想让它看起来好看,我们需要使用for循环。

for name in names:
    print( name ) # You can change this line to print( name, end=' ' ) 
                  # If you want all the names on one line.

这将产生如下结果:

Spam
Eggs
Waheed

这看起来比只打印列表要好得多。

完整(固定)代码:

names = []
def displayMenu():
    print(" 1. Add Name")
    print(" 2. Show list")
    print(" 3. Quit")
    choice = int(input("Enter your choice : "))
    while 3 < choice < 1: # Only accept choices in between 3 and 1
        choice = input("Invalid. Re-enter your choice: ")
    return choice

def addname():
    number = int(input('How many names would you like to enter: '))
    while number > 10: # Don't allow any numbers under 10
        number = int(input('Please enter a number under 10: '))
    for i in range(number):
        name = input('Enter name: ')
        names.append(name)

def displayData():
    for name in names:
        print( name ) # You can change this line to print( name, end=' ' ) 
                      # If you want all the names on one line.

option = displayMenu()

while option != 3:
    if option == 1:
       addname()
    elif option == 2:
        displayData()
    option = displayMenu()

print("Program terminating")