簡體   English   中英

While循環使用計數器

[英]While Loop using a counter

我試圖使它在用戶使用while循環和中斷功能輸入來賓姓名的地方,當循環結束時我希望列表按編號順序打印出來

  1. 詹姆士
  2. 卡爾
  3. 麗莎
  4. 布萊恩

但是對可以輸入多少個名稱沒有設置限制

我已經嘗試了while真實循環,但是當我使用

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

您基本上會在每次循環迭代中覆蓋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

希望能幫助到你。

如注釋中所述,您不會在任何地方保存數據。 我使用enumerate編寫了代碼,這消除了您情況下對count變量的需要。 您可以在文檔中閱讀。 該代碼是

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,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM