我试图让用户使用 while 循环和break函数输入客人姓名,当循环结束时,我希望列表按编号顺序打印出来,例如
- 詹姆斯
- 卡尔
- 丽莎
- 布莱恩
但是对于可以输入的名称数量没有设定限制
我已经尝试过 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.名称
您可以使用以下代码来完成它:
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
我是一名优秀的程序员,十分优秀!