使用循环保留多个用户输入

问题描述 投票:0回答:1

我想编写一个小程序,向用户询问他们最好的朋友的名字。完成后,按Enter键,循环将停止并在编号列表中打印每个朋友的姓名。

它应该看起来像这样:

  • 输入您最好的朋友的名字
  • 朋友名称(输入以退出):Friend1
  • 朋友名称(输入以退出):Friend2
  • 朋友名称(输入退出):输入
  • 我最好的朋友是:
    1. Friend1
    2. Friend2

这是我到目前为止的内容,是不完整的草稿:

while True:
    friendname = input("Friends Name (Enter to quit): ")
    list = str(friendname)" #something here keeps the names
    if friendname == "":
        break
print (list)

问题是,我不知道如何保留名称,然后将其打印在列表中。

python
1个回答
0
投票
friends = input("Enter your friends' names separated by a space: ").split() print("Your friends are: {}".format(friends))

这里是一个例子:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.