我正在嘗試創建一個程式,該程式允許輸入 3 個宇航員姓名,然后執行 10 秒的起飛倒計時。我希望它僅在輸入 3 個名稱時才倒計時,如果少于,則退出或再次詢問。在發射時,祝愿每位宇航員“一路順風”。
#Create Blast Off program#
print("-----------------")
print("Blast Off Program")
print("-----------------")
print("")
print("Welcome to the NASA SHuttle Launch Facility 1.0")
print("To proceed you must enter 3 astrounat's names:")
print("")
names = []
counter = 0
#loop until 3 astronauts names are entered#
while counter !=3:
counter = counter 1
#ask the user to add astronauts name to the list#
name1 = input("Astronaut name: ").title()
names.append(name1)
print("")
for items in names:
print("Astronaut's name", counter, ":", name1)#prints out the names#
print("")
print ("You have entered 3 names. The system is now live and the countdown will commence")
for i in range(10,-1, -1):#starts at 10, end at 0. reduces number by 1 each time
print(i)
else:
print(" exit program or ask again")
print("BLAST OFF")
print("Bon Voyage and Good Luck to our brave astronauts:")
for items in names:
print("Astronaut's name:", name1)`
uj5u.com熱心網友回復:
這應該涵蓋您以更 Pythonic 的方式提出的所有觀點。
- 它將停留在 while 回圈中,直到為 name 給出三個非空字串
- 它倒計時
- 它將親自祝愿每位宇航員一路平安
# Create Blast Off program
print("""\
Blast Off Program
-----------------
Welcome to the NASA SHuttle Launch Facility 1.0
To proceed you must enter 3 astrounat's names:
-----------------
""")
names = []
while len(names) < 3: # loop until 3 astronauts names are entered
name = input("Astronaut name: ") # ask the user to add astronauts
if name: # empty string is falsy, if empty don't append
names.append(name.title())
print()
for i, name in enumerate(names):
print("Astronaut's name", i, ":", name) # print out the names
print()
print("You have entered 3 names. The system is now live and the countdown will commence")
for i in range(10, -1, -1): # starts at 10, end at 0. reduces number by 1 each time
print(i)
print()
print("BLAST OFF")
for name in names:
print("Bon Voyage and Good Luck to :", name)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416889.html
標籤:
上一篇:回圈遍歷行永遠不會結束
