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

时间:2013-10-22 01:55:14

标签: 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包括收入计算函数(calcIncome),它将售票和座位成本作为传递参数并返回为该部分生成的收入。

•务必使用currencyFormat

1 个答案:

答案 0 :(得分:1)

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

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?

现在我们已经解决了这个问题,让我们来解决问题:

  

显示每个部分的生成收入以及剧院总数。

当前编写代码的方式很简单,因为您只调用main一次,而main只处理部分。对于除一个部分以外的所有部分,答案总是0

但是让我们看看如果我们假设您连续致电main直到满足某些条件会发生什么:

def main():
    while section:
        section = (input('What section are you inputing ticket numbers for?'))
        if section in 'Aa':
            limit = 300
            cost = 20
    elif section in 'Bb':
        limit = 500
        cost = 15
    if section in 'Cc':
        limit = 200
        cost = 10
    ticketSold = getTickets(section, limit)
    calcIncome(ticketSold, cost)

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

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。)