当使用coun循环时

2024-06-01 07:45:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让一个用户使用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名称 三。名称 4姓名


Tags: to名称inputcounter名字extraare姓名
3条回答

如注释中所述,您没有将数据保存到任何地方。我已经写了一个代码使用枚举,消除了您的情况下计数变量的需要。你可以在文档中看到。代码是

代码

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

attendes = []  # Keep names of guest

flag = True  # To keep loop control

while 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 = False

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

# No need for count when using enumerate
for 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 wedding

Guest's Name(or press Enter to quit): A
Guest's Name(or press Enter to quit): B
Guest's Name(or press Enter to quit): C
Guest's Name(or press Enter to quit): D
Guest's Name(or press Enter to quit):

My Wedding Attendees are:

1. A, 2. B, 3. C, 4. D,

您可以使用以下代码来完成:

counter = 1
extra=[' ']  #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 += 1

print("\nMy Wedding Attendees are: "  "\n")
a.pop(-1) #removing the '' from the list, as we do not need it 

j=1
for i in range(0,len(a)):
    print(str(j)+'. '+a[i])
    j+=1

基本上在每个循环迭代中都覆盖extra,这就是为什么不打印名称的原因。我建议你把所有客人的名字(有指定的柜台)收集在一个列表中,然后打印出来。例如:

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

counter = 0

guest_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 names
print("\nMy wedding attendees are:\n" + " ".join(guest_names))

输出将(在Jupyter笔记本中测试):

Now enter the names of other people you are inviting to your wedding

Guest's name (or press Enter to quit): John
Guest's name (or press Enter to quit): Marie
Guest's name (or press Enter to quit): Sebbe
Guest's name (or press Enter to quit): David
Guest'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. John
2. Marie
3. Sebbe
4. David

希望有帮助。你知道吗

相关问题 更多 >