我的代码执行乘客摘要太早了

huangapple go评论67阅读模式
英文:

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(&quot;Welcome to Kevin&#39;s Travel Service!&quot;)
def myname():
print(&#39;Written by: Kevin B&#39;)
print(&#39;Written on date: Feb 26,2023&#39;)
def stops_prices():
#Prompt user on where they are stopping at
print(&quot;\nThe stops for the bus stops at are Baltimore, Philiadelphia, and New York.\n&quot;)
print(&#39;The price from Washington to Baltimore is: $&#39; + str(wash_to_balt_adult),&#39;for adults, $&#39;+ str(wash_to_balt_child),&#39;for children.\n&#39;)
print(&#39;The price from Washington to Philiadelphia is: $&#39; + str(wash_to_philly_adult),&#39;for adults, $&#39; + str(wash_to_philly_child),&#39;for children.\n&#39;)
print(&#39;The price from Washington to New York is: $&#39; + str(wash_to_ny_adult),&#39;for adults, $&#39; + str(wash_to_ny_child),&#39;for children.\n&#39;)
def passinfo():
validname = False
adultcount = &quot;&quot;
childcount = &quot;&quot;
# Prompt user on where they are departing bus to
stop = int(input(&quot;\n\nWhere are you traveling to? Please enter 1 for Baltimore, 2 for Philadelphia, or 3 for New York, &lt;Enter&gt; to exit:\t&quot;))
# Validate stop
while not validname:
if stop &lt; 1 or stop &gt; 3:
stop = int(input(&quot;Invalid number. Please enter 1 for Baltimore, 2 for Philadelphia, or 3 for New York: &quot;))
else:
validname = True
adultcount = int(input(&quot;How many adults? &quot;))
adultnames = []
for number in range(adultcount):
print(&quot;\nAdult&quot;, number+1)
name = input(&quot;What is the passenger&#39;s name? &quot;)
adultnames.append(name)
childcount = int(input(&quot;\nHow many children? &quot;))
childnames = []
for number in range(childcount):
print(&quot;\nChild&quot;, number+1)
name = input(&quot;What is the passenger&#39;s name? &quot;)
childnames.append(name)
print (&#39;\n# of Adults: &#39;,adultcount,&#39;\n# of Children: &#39;,childcount,&#39;\nWhich Stop: &#39;,stop,&#39;\nName of Adults: &#39;,adultnames,&#39;\nName of Children: &#39;,childnames,&#39;\n&#39;)
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(&#39;Total cost of adult tickets: &#39;,adultprice)
# Calculate the cost for children going to Baltimore
childprice = childcount * wash_to_balt_child
print(&#39;Total cost of childrens tickets: &#39;,childprice)
totalprice = adultprice + childprice
print(&#39;Total cost of all tickets: &#39;,totalprice)
elif stop == 2:
# Calculate the cost for adults going to Philadelphia
adultprice = adultcount * wash_to_philly_adult
print(&#39;Total cost of adult tickets: &#39;,adultprice)
# Calculate the cost for children going to Philadelphia
childprice = childcount * wash_to_philly_child
print(&#39;Total cost of childrens tickets: &#39;,childprice)
totalprice = adultprice + childprice
print(&#39;Total cost of all tickets: &#39;,totalprice)
elif stop == 3:
# Calculate the cost for adults going to New York
adultprice = adultcount * wash_to_ny_adult
print(&#39;Total cost of adult tickets: &#39;,adultprice)
childprice = childcount * wash_to_ny_child
print(&#39;Total cost of childrens tickets: &#39;,childprice)
totalprice = adultprice + childprice
print(&#39;Total cost of all tickets: &#39;,totalprice)
main()
welcome()
print(&#39;\n&#39;)
myname()
print(&#39;\n&#39;)
passinfo()
print(&#39;\n&#39;)

答案1

得分: 1

你应该取消缩进你不希望在循环内的代码

你目前的说法:

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

    passengerprices(adultcount,childcount,stop)

请尝试这样:

for number in range(childcount):
    print(&quot;\nChild&quot;, number+1)
    name = input(&quot;What is the passenger&#39;s name? &quot;)
    childnames.append(name)
  
print (&#39;\n# of Adults: &#39;,adultcount,&#39;\n# of Children: &#39;,childcount,&#39;\nWhich Stop: &#39;,stop,&#39;\nName of Adults: &#39;,adultnames,&#39;\nName of Children: &#39;,childnames,&#39;\n&#39;)
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(&quot;\nChild&quot;, number+1)
name = input(&quot;What is the passenger&#39;s name? &quot;)
childnames.append(name)
print (&#39;\n# of Adults: &#39;,adultcount,&#39;\n# of Children: &#39;,childcount,&#39;\nWhich Stop: &#39;,stop,&#39;\nName of Adults: &#39;,adultnames,&#39;\nName of Children: &#39;,childnames,&#39;\n&#39;)
passengerprices(adultcount,childcount,stop)

Try this:

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

huangapple
  • 本文由 发表于 2023年2月26日 20:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定