英文:
My code is executing the passenger summary too soon
问题
以下是代码的翻译部分:
我的火车程序正在正确运行并进行正确的计算,但在我有机会输入多个孩子的名字之前,它就会过早地触发乘客车票摘要信息。
尽管它列出的数据并没有错误,但我希望在给出总数之前,仅在一次之前触发成人人数、儿童人数、停靠信息、成人票数和儿童票数的摘要。
# 这个程序将计算从华盛顿到纽约的巴士上所有乘客(成人和儿童)的数量。
# 这个程序还将计算在巴尔的摩和费城停靠的巴士次数。
# 显示姓名、类别和日期的函数
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("欢迎来到Kevin的旅行服务!")
def myname():
print('作者:Kevin B')
print('编写日期:2023年2月26日')
def stops_prices():
# 提示用户巴士停靠的地方
print("巴士停靠的地方包括巴尔的摩、费城和纽约。")
print('从华盛顿到巴尔的摩的价格为:$' + str(wash_to_balt_adult), '成人,$' + str(wash_to_balt_child), '儿童。')
print('从华盛顿到费城的价格为:$' + str(wash_to_philly_adult), '成人,$' + str(wash_to_philly_child), '儿童。')
print('从华盛顿到纽约的价格为:$' + str(wash_to_ny_adult), '成人,$' + str(wash_to_ny_child), '儿童。')
def passinfo():
validname = False
adultcount = ""
childcount = ""
# 提示用户他们要前往哪个目的地
stop = int(input("\n\n您要前往哪里?请输入1表示巴尔的摩,2表示费城,3表示纽约,按<Enter>退出:\t"))
# 验证目的地
while not validname:
if stop < 1 or stop > 3:
stop = int(input("无效数字。请输入1表示巴尔的摩,2表示费城,3表示纽约:"))
else:
validname = True
adultcount = int(input("成人有多少人?"))
adultnames = []
for number in range(adultcount):
print("\n成人", number+1)
name = input("乘客的名字是什么?")
adultnames.append(name)
childcount = int(input("\n有多少儿童?"))
childnames = []
for number in range(childcount):
print("\n儿童", number+1)
name = input("乘客的名字是什么?")
childnames.append(name)
print ('成人人数:', adultcount, '儿童人数:', childcount, '目的地:', stop, '成人姓名:', adultnames, '儿童姓名:', childnames)
passengerprices(adultcount, childcount, stop)
def passengerprices(adultcount, childcount, stop):
if stop == 1:
# 计算前往巴尔的摩的成人成本
adultprice = adultcount * wash_to_balt_adult
print('成人票的总成本:', adultprice)
# 计算前往巴尔的摩的儿童成本
childprice = childcount * wash_to_balt_child
print('儿童票的总成本:', childprice)
totalprice = adultprice + childprice
print('所有车票的总成本:', totalprice)
elif stop == 2:
# 计算前往费城的成人成本
adultprice = adultcount * wash_to_philly_adult
print('成人票的总成本:', adultprice)
# 计算前往费城的儿童成本
childprice = childcount * wash_to_philly_child
print('儿童票的总成本:', childprice)
totalprice = adultprice + childprice
print('所有车票的总成本:', totalprice)
elif stop == 3:
# 计算前往纽约的成人成本
adultprice = adultcount * wash_to_ny_adult
print('成人票的总成本:', adultprice)
childprice = childcount * wash_to_ny_child
print('儿童票的总成本:', childprice)
totalprice = adultprice + childprice
print('所有车票的总成本:', totalprice)
main()
welcome()
print('\n')
myname()
print('\n')
passinfo()
print('\n')
希望这些翻译对您有帮助。
英文:
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("\n\nWhere 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')
答案1
得分: 1
你应该取消缩进你不希望在循环内的代码
你目前的说法:
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)
请尝试这样:
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论