如何重复此操作并保存每个部分的结果?

2024-10-03 23:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

def main():
    section = 'A'
    while section:
        section = (input('What section are you inputing ticket numbers for? '))
        if section in ('a', 'A') :
            limit = 300
            cost = 20
            ticketSold = getTickets(section, limit)
            sectionACost = calcIncome(ticketSold, cost)
        elif section in ('b', 'B'):
            limit = 500
            cost = 15
            ticketSold = getTickets(section, limit)
            sectionBCost = calcIncome(ticketSold, cost)
        elif section in ('c', 'C'):
            limit = 200
            cost = 10
            ticketSold = getTickets(section, limit)
            sectionBCost = calcIncome(ticketSold, cost)
    displayTotals()



def getTickets(section, limit):
    ticketSold = int(input('How many tickets were sold? '))
    return int(ticketSold)
    if ticketsValid(ticketSold, limit):
         return ticketsValid

def ticketsValid(ticketSold, limit):
    if ticketSold <= limit:
        status = True
    else:
        status = False
    return status

def calcIncome(ticketSold, cost):
    sectionIncome = ticketSold * cost
    return sectionIncome

 def displayTotals():
     print('Section A generated {}'.format(sectionACost))
    print('Section B generated {}'.format(sectionBCost))
    print('Section C generated {}'.format(sectionCCost))
    totalRevenue = sectionACost + sectionBCost + sectionCCost
    print('The total revenue is {}'(totalRevenue))


main()

我的问题是循环不能退出来显示总数

其他问题要求:

•创建一个主模块作为程序启动模块

•在主模块中使用本地命名常量,用于座位成本和章节座位限制,这些将传递给以下各点所述的模块和功能。在

•尽管问题中有三个座位区,但将根据传递的参数创建一组通用模块和函数,用于任何给定的部分。这些功能的一般逻辑描述如下:

o包含输入函数(getTickets),该函数将给定部分的节字母和座位限制作为参数。返回给定区段的票数。此函数只应返回有效票证数;应从该函数调用验证函数ticketsValid。在

o包括验证功能(ticketsValid),该功能将每节售出的车票和每节的座位限制作为通过的参数。返回一个布尔值,该布尔值指示为给定部分售出的已通过票证是否在有效范围内。此验证函数将从先前定义的getTickets函数中调用。在

o包含收入计算功能(Calincome),该功能将售出的机票和座位成本作为传递的参数,并返回该部分产生的收入。在

•确保使用currencyFormat


Tags: 模块函数功能returndefsectionlimitprint
1条回答
网友
1楼 · 发布于 2024-10-03 23:23:12

在我解决您的主要问题之前,该代码中存在一些问题:

def main():  
    section = (input('What section are you inputing ticket numbers for? '))
    if section == 'a' or 'A': # You mean if section in 'Aa':
        limit = 300
        cost = 20
    if section == 'b' or 'B': # You mean elif section in 'Bb':
        limit = 500
        cost = 15
    if section == 'c' or 'C': # You mean elif section in 'Cc':
        limit = 200
        cost = 10
    ticketSold = getTickets(section, limit)
    calcIncome(ticketSold, cost)

def getTickets(section, limit): # Doesn't actually use section.
    ticketSold = int(input('How many tickets were sold? '))
    return int(ticketSold) # Everything after this is dead code.
    if ticketsValid(ticketSold, limit): # Even if this did get called, 
        return ticketsValid # What should it return if the tickets aren't valid? 0?

既然我们已经解决了这个问题,让我们来回答这个问题:

Display the generated income for each section as well as the theater total.

当前编写代码的方式很简单,因为您只调用main一次,并且只对部分调用main进程。答案总是指向0,只有一个部分除外。在

但是,让我们看看如果我们假设您连续调用main直到满足某个条件,会发生什么情况:

^{pr2}$

好吧,我们可能会看到一些相当复杂的解决方案,但是基于这段代码,我将假设最简单的事情:每个部分一个变量。我们可以用全球名字

section_a_cost = 0
section_b_cost = 0
section_c_cost = 0

我们需要一个函数来显示最终总数:

def displayTotals():
    '''display the totals'''
    print('Section A generated {}'.format(section_a_cost)
    print('Section B generated {}' …) # I leave the rest to you.
    # How do we generate the total for the theater?

def main():
    section = 'A' # whatever, just to get us started
    while section:
        section = input('What section are you inputing ticket numbers for? ')
        if section in 'Aa':
            limit = 300
            cost = 20
            ticketSold = getTickets(section, limit)
            section_a_cost += calcIncome(ticketSold, cost)
        elif section in 'Bb':
            limit = 500
            cost = 15
            ticketSold = getTickets(section, limit)
            section_b_cost += calcIncome(ticketSold, cost)
        elif …
            …
    displayTotals()

(旁白:如果不清楚,我这里的目标并不是解决现实问题的最佳解决方案——我在假设OP在课程中的位置,并试图通过使用复杂的数据结构或聚合器来避免跳得太远。我也试图不给出完整的答案,而是在vein of how to answer a homework question中回答。)

相关问题 更多 >