如果串列的長度大于 1 人,我只是希望能夠移動到 else 塊,當然,如果只有一個人來,我所說的話將不適合說如果 2 人或更多人來
guests = []
guests_potential = []
attendence_question = input("Are you going to be coming to my house?\n")
while attendence_question == 'yes':
name = input("Ok! Can I please have your name?\n" )
guests.append(name)
#guests_integer = " ".join(guests)
if len(guests) >= 1:
print("Great! So far it is only you that is coming")
print("----------------")
attendence_question = input("Hi!\nHow about you?\nAre you going to be coming to my house?\n")
guests.append(name)
else:
if len(guests) < 1:
for number, guest in enumerate(guests, 1):
print(number, guest)
guests_potential.append(name)
print("Ok! I've added your name list to the potential attendees!")
uj5u.com熱心網友回復:
您的if宣告總是被呼叫,因為它正在檢查來賓串列中是否至少有 1 個名字。情況總是如此,因為在上一行中,您將姓名添加到客人串列中。
相反,只需檢查是否有 1 個客人,否則執行 else 回圈:
while attendence_question == 'yes':
name = input("Ok! Can I please have your name?\n" )
guests.append(name)
if len(guests) == 1:
print("Great! So far it is only you that is coming")
print("----------------")
else:
print("The current guest list is:")
for number, guest in enumerate(guests):
print(number, guest)
attendence_question = input("Hi!\nHow about you?\nAre you going to be coming to my house?\n"))
uj5u.com熱心網友回復:
你的 else 塊永遠不會做任何可見的事情,因為它包含一個if陳述句來檢查少于一個客人(所以,零)然后回圈這些零客人
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388034.html
標籤:Python
上一篇:Python:滾動計算置信區間
