You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python巴士票务程序问题:乘客摘要过早输出

问题分析与修复方案

问题出在passinfo函数的儿童姓名输入循环里——你把打印乘客摘要和计算费用的代码写在了循环内部,导致每输入一个儿童姓名,程序就会输出一次摘要并计算费用,而不是等所有儿童信息都收集完再执行。

具体修改点

把以下两行代码:

print ('\n成人数量:',adultcount,'\n儿童数量:',childcount,'\n目的地站点:',stop,'\n成人姓名:',adultnames,'\n儿童姓名:',childnames,'\n')
passengerprices(adultcount,childcount,stop)

从childnames的for循环中移出,放到循环结束之后(也就是for number in range(childcount):的闭合大括号外面)。

修正后的完整代码

# 该程序将计算从华盛顿前往纽约的巴士乘客(成人和儿童)数量
# 同时计算经停巴尔的摩和费城的巴士行程费用

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():
    # 提示用户选择目的地站点
    print("\n巴士经停站点为巴尔的摩、费城和纽约。\n")
    print('华盛顿至巴尔的摩的票价:成人$' + str(wash_to_balt_adult) + ',儿童$'+ str(wash_to_balt_child)+'\n')
    print('华盛顿至费城的票价:成人$' + str(wash_to_philly_adult) + ',儿童$' + str(wash_to_philly_child)+'\n')
    print('华盛顿至纽约的票价:成人$' + str(wash_to_ny_adult) + ',儿童$' + str(wash_to_ny_child)+'\n')

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 ('\n成人数量:',adultcount,'\n儿童数量:',childcount,'\n目的地站点:',stop,'\n成人姓名:',adultnames,'\n儿童姓名:',childnames,'\n')
    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')

内容的提问来源于stack exchange,提问作者JRN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 05:22:53