编写此代码的正确方法是什么?

what would be the correct way to write this code?

我正在尝试创建一个允许输入 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)`

这应该涵盖了你以更 pythonic 的方式提出的所有观点。

  • 它将停留在 while 循环中,直到为名称给出三个非空字符串
  • 它倒数
  • 亲自祝福每位宇航员一路顺风
# 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)