使用计数器的 While 循环

我试图让它在用户使用while循环和break函数输入客人姓名的地方,当循环结束时,我希望列表以编号顺序打印出来

  1. 詹姆士

  2. 卡尔

  3. 丽莎

  4. 布莱恩

但是可以输入多少个名字没有限制

我已经尝试过 while true 循环,但是当我使用

print("\nMy Wedding Attendees are: "  "\n", extra)

它只打印出我的婚礼参加者是:但不是名字

print("Now enter the names of other people you are inviting to your wedding\n")

counter = 1

extra = input("Guest's Name(or press Enter to quit): ")

while counter >= 1:       #WHILE loop

    extra = input("Guest's Name(or press Enter to quit): ")

    counter = counter + 1

    if extra == "":

        break     #break Statement

print("\nMy Wedding Attendees are: "  "\n", extra)

我希望它看起来像


我的婚礼参加者是: 1. 姓名 2. 姓名 3. 姓名 4. 姓名


RISEBY
浏览 194回答 3
3回答

侃侃尔雅

您可以使用以下代码完成它:counter = 1extra=[' ']  #initializing a random value for extra at the start.a=[]while extra != '':          extra = input("Guest's Name(or press Enter to quit): ")     a.append(extra)     counter += 1print("\nMy Wedding Attendees are: "  "\n")a.pop(-1) #removing the '' from the list, as we do not need it j=1for i in range(0,len(a)):    print(str(j)+'. '+a[i])    j+=1

红糖糍粑

如评论中所述,您没有将数据保存在任何地方。我已经使用 enumerate 编写了一个代码,它消除了您对 count 变量的需求。您可以在文档中阅读有关它的信息。代码是代码print("Now enter the names of other people you are inviting to your wedding\n")attendes = []  # Keep names of guestflag = True  # To keep loop controlwhile flag is True:    name = input("Guest's Name(or press Enter to quit): ")    if name is not "":  # If not empty then add to list        attendes.append(name)    else:  # stop the loop        flag = Falseprint("\nMy Wedding Attendees are: "  "\n")# No need for count when using enumeratefor no, name in enumerate(attendes, 1):    print('{}. {}'.format(no, name), end=', ')    # If you don't want the , in output just use end=' '给出输出Now enter the names of other people you are inviting to your weddingGuest's Name(or press Enter to quit): AGuest's Name(or press Enter to quit): BGuest's Name(or press Enter to quit): CGuest's Name(or press Enter to quit): DGuest's Name(or press Enter to quit):My Wedding Attendees are:1. A, 2. B, 3. C, 4. D,

慕斯王

您基本上extra在每次循环迭代中都会覆盖,这就是为什么不打印名称的原因。我建议您将所有客人姓名(带有指定的柜台)收集在一个列表中,然后打印出来。例如:print("Now enter the names of other people you are inviting to your wedding\n")counter = 0guest_names = list()while True:    guest_name = input("Guest's name (or press Enter to quit): ")    if guest_name == "":        break    counter += 1    guest_names.append(str(counter) + ". " + guest_name)  # record all namesprint("\nMy wedding attendees are:\n" + " ".join(guest_names))输出将是(在 Jupyter notebook 中测试):Now enter the names of other people you are inviting to your weddingGuest's name (or press Enter to quit): JohnGuest's name (or press Enter to quit): MarieGuest's name (or press Enter to quit): SebbeGuest's name (or press Enter to quit): DavidGuest's name (or press Enter to quit): My Wedding Attendees are:1. John 2. Marie 3. Sebbe 4. David如果您想要名称之间的换行符,您应该使用换行符分隔符join():print("\nMy wedding attendees are:\n" + "\n".join(guest_names))在这种情况下,输出将是:My wedding attendees are:1. John2. Marie3. Sebbe4. David希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python