You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将用户多个输入添加到列表?给定代码如何实现多输入?

Hey there! Let's tackle your questions one by one.

How to add multiple user inputs to a list

The core idea is to use a loop to keep asking for input until the user decides to stop, then add each valid input to your list. Common stop triggers include entering an empty string (just pressing Enter), or a specific keyword like "quit".

How your provided code implements multi-input functionality

Let's walk through each line of your code to understand how it works:

names = [] # Here we define an empty list.
while True:
    eingabe = input('Please enter a name: ')
    if not eingabe:
        break
    names.append(eingabe)
    print(eingabe)
  • names = []: First, we create an empty list to store all the names the user inputs.
  • while True: This sets up an infinite loop—it'll keep running until we hit a break command.
  • eingabe = input('Please enter a name: '): We prompt the user to enter a name, and store their input in the eingabe variable (that's German for "input", by the way).
  • if not eingabe: break: We check if the input is empty (meaning the user just pressed Enter without typing anything). If that's the case, we exit the loop.
  • names.append(eingabe): If the input isn't empty, we add the entered name to our names list.
  • print(eingabe): This line just gives immediate feedback by printing the name the user just entered (it's optional, but helpful for confirming the input was received).

How to use this code

  1. Run the script—you'll see the prompt Please enter a name: .
  2. Type a name and press Enter; the name will be added to the names list, and you'll see it printed back to you.
  3. Repeat step 2 for as many names as you want to add.
  4. When you're done, just press Enter without typing anything—this triggers the break and stops the loop.

If you want to see all the names you collected at the end, you can add a line after the loop:

print("All entered names:", names)

内容的提问来源于stack exchange,提问作者Aria Bhej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:05:43