Home > Back-end >  what would be the correct way to write this code?
what would be the correct way to write this code?

Time:01-21

I am trying to create a program that allows the entry of 3 astronaut names and then performs a 10 second countdown for take-off. I want it to only countdown if 3 names are entered, if less, exit or ask again. And on launch, wish ‘Bon Voyage’ to each astronaut by name.

#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)`

CodePudding user response:

This should cover all the points you have made in a more pythonic way.

  • it will stay in the while loop until three non empty strings are given for name
  • it does the count down
  • it will wish bon voyage to each astronaut personally
# 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)
  •  Tags:  
  • Related