My code is executing the passenger summary too soon

Question:

My train program is working correctly and doing the correct calculations however it is firing the passenger ticket summary information too soon before I have a chance to enter more than one child’s name.

Although the data it lists is not wrong I want the summary of Number of adults number of kids, stop info, count of adult tickets and count of children’s tickets to fire only once right before the totals are given.

#This program will calculate all the passengers(adults and kids)that ride the bus from Washingtion to New York
#This program will also calculate the bus rides with stops at Baltimore and Philadelphia

#Function that displays name, class, and date

def main():
 passname=None
 passadult=None
 passchild=None
 morepassengers=True
 names=list()
 adultcounts=list()
 childcounts=list()
 totals=list()
 stops=list()
 adultprices=list()
 childprices=list()
 totalprices=list()

wash_to_balt_adult = 20
wash_to_balt_child = 10
wash_to_philly_adult = 25
wash_to_philly_child = 10
wash_to_ny_adult = 30
wash_to_ny_child = 15

def welcome():
    print("Welcome to Kevin's Travel Service!")

def myname():
    print('Written by: Kevin B')
    print('Written on date: Feb 26,2023')

def stops_prices():

        #Prompt user on where they are stopping at
        print("nThe stops for the bus stops at are Baltimore, Philiadelphia, and New York.n")
        print('The price from Washington to Baltimore is: $' + str(wash_to_balt_adult),'for adults, $'+ str(wash_to_balt_child),'for children.n')

        print('The price from Washington to Philiadelphia is: $' + str(wash_to_philly_adult),'for adults, $' + str(wash_to_philly_child),'for children.n')

        print('The price from Washington to New York is: $' + str(wash_to_ny_adult),'for adults, $' + str(wash_to_ny_child),'for children.n')


def passinfo():
    validname = False
    adultcount = ""
    childcount = ""

    # Prompt user on where they are departing bus to
    stop = int(input("nnWhere are you traveling to? Please enter 1 for Baltimore, 2 for Philadelphia, or 3 for New York, <Enter> to exit:t"))

    # Validate stop
    while not validname:
        if stop < 1 or stop > 3:
            stop = int(input("Invalid number. Please enter 1 for Baltimore, 2 for Philadelphia, or 3 for New York: "))
        else:
            validname = True

    adultcount = int(input("How many adults? "))
    adultnames = []
    for number in range(adultcount):
        print("nAdult", number+1)
        name = input("What is the passenger's name? ")
        adultnames.append(name)

    childcount = int(input("nHow many children? "))
    childnames = []
    for number in range(childcount):
        print("nChild", number+1)
        name = input("What is the passenger's name? ")
        childnames.append(name)
      
        print ('n# of Adults: ',adultcount,'n# of Children: ',childcount,'nWhich Stop: ',stop,'nName of Adults: ',adultnames,'nName of Children: ',childnames,'n')

        passengerprices(adultcount,childcount,stop)
        
def passengerprices(adultcount,childcount,stop):
    if stop == 1:
              # Calculate the cost for adults going to Baltimore
              adultprice = adultcount * wash_to_balt_adult
              print('Total cost of adult tickets: ',adultprice)
              # Calculate the cost for children going to Baltimore
              childprice = childcount * wash_to_balt_child
              print('Total cost of childrens tickets: ',childprice)
              totalprice = adultprice + childprice
              print('Total cost of all tickets: ',totalprice)
    
    elif stop == 2:
              # Calculate the cost for adults going to Philadelphia
              adultprice = adultcount * wash_to_philly_adult
              print('Total cost of adult tickets: ',adultprice)
              # Calculate the cost for children going to Philadelphia
              childprice = childcount * wash_to_philly_child
              print('Total cost of childrens tickets: ',childprice)
              totalprice = adultprice + childprice
              print('Total cost of all tickets: ',totalprice)
    
    elif stop == 3:
              # Calculate the cost for adults going to New York
              adultprice = adultcount * wash_to_ny_adult
              print('Total cost of adult tickets: ',adultprice)
              childprice = childcount * wash_to_ny_child
              print('Total cost of childrens tickets: ',childprice)
              totalprice = adultprice + childprice
              print('Total cost of all tickets: ',totalprice)

main()
welcome()
print('n')
myname()
print('n')
passinfo()
print('n')
Asked By: JRN

||

Answers:

You should de-indent the code you want to not be inside the loop

You currently say:

for number in range(childcount):
    print("nChild", number+1)
    name = input("What is the passenger's name? ")
    childnames.append(name)
  
    print ('n# of Adults: ',adultcount,'n# of Children: ',childcount,'nWhich Stop: ',stop,'nName of Adults: ',adultnames,'nName of Children: ',childnames,'n')

    passengerprices(adultcount,childcount,stop)

Try this:

for number in range(childcount):
    print("nChild", number+1)
    name = input("What is the passenger's name? ")
    childnames.append(name)
  
print ('n# of Adults: ',adultcount,'n# of Children: ',childcount,'nWhich Stop: ',stop,'nName of Adults: ',adultnames,'nName of Children: ',childnames,'n')
passengerprices(adultcount,childcount,stop)
Answered By: Eureka
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.