简体   繁体   English

布尔输入的函数不打印预期结果

[英]Function with boolean input not printing the expected result

Sorry to bother you at this hour, but I am really struggling with a program. 很抱歉在这个时候打扰你,但我真的很挣钱。 I am working on a python project and I have a very specific set of instructions to follow. 我正在开发一个python项目,我有一组非常具体的指令要遵循。 Use local named constants in the main module for seat costs and section seat limits that will be passed to the modules and functions described under the following points. 在主模块中使用本地命名常量来获取座位成本和部分座位限制,这些限制将传递给在以下几点下描述的模块和功能。 These are the instructions. 这些是说明。

• Although there are three seating sections in the problem, a set of generic functions will be created that are used for any given section based upon the passed parameters. •虽然问题中有三个座位部分,但将创建一组通用函数,这些函数基于传递的参数用于任何给定的部分。 These functions will be called once for each section: A, B, and C. The general logic for these function is described below: 这些函数将针对每个部分调用一次:A,B和C.这些函数的一般逻辑如下所述:

1.)Include input function (getTickets) that takes section letter and seat limit for given section as parameters. 1.)包括输入函数(getTickets),它将给定部分的分节字母和座位限制作为参数。 The number of tickets for the given section is returned. 返回给定部分的票数。 This function should only return a valid number of tickets; 此功能只应返回有效数量的票证; the validation function, ticketsValid, should be called from this function. 应该从此函数调用验证函数ticketsValid。

2.)Include validation function (ticketsValid) that takes the tickets sold per section and seat limit per section as passed parameters. 2.)包括验证功能(ticketsValid),它将每个部分销售的票证和每个部分的座位限制作为传递的参数。 A Boolean indicating whether the passed tickets sold for the given section is in the valid range is returned. 返回一个布尔值,指示为给定部分销售的传递的票证是否在有效范围内。 This validation function will be called from within the previously defined getTickets function. 将在先前定义的getTickets函数内调用此验证函数。

3.)Include income calculation function (calcIncome) that takes tickets sold and seat cost as passed parameters and returns the income generated for the section. 3.)包括收入计算函数(calcIncome),它将售票和座位成本作为传递参数并返回为该部分生成的收入。

Now my problem is, I cannot understand why my validator is not working properly. 现在我的问题是,我无法理解为什么我的验证器工作不正常。 I have it inside another function, but when I run the code it just skips the validating all together. 我把它放在另一个函数中,但是当我运行代码时它只是一起跳过验证。 This is my code, any help is appreciated. 这是我的代码,任何帮助表示赞赏。 Again thanks for helping so late at night! 再次感谢你帮助这么深夜! :) :)

def Main():

    aPrice=20
    bPrice=15
    cPrice=10
    alimit=100
    blimit=500
    climit=200
    ticketSold=0
    getTickets(alimit)
    calcIncome(ticketSold,aPrice)
    getTickets(blimit)
    calcIncome(ticketSold,bPrice)
    getTickets(alimit)
    calcIncome(ticketSold,cPrice)


def getTickets(limit):

    ticketSold =int(input("How many tickets were sold in section? "))
    ticketsValid(ticketSold,limit)
    return ticketSold

def ticketsValid(ticketSold,limit):

    if (ticketSold>limit | ticketSold<0):
        print ("ERROR: Section A needs to be a number between 0 and 300")

def calcIncome(ticketSold,price):

    totalIncome= (price*ticketSold)
    print ("The total income for the theater section is "+str(totalIncome))

Main()
  1. Here: ticketSold>limit | ticketSold<0 这里: ticketSold>limit | ticketSold<0 ticketSold>limit | ticketSold<0 change | ticketSold>limit | ticketSold<0更改| to or and it will work. or它会起作用。
  2. Your calcIncome gets ticketSold = 0 , not the newly calculated income. 您的calcIncome获得ticketSold = 0 ,而不是新计算的收入。 This is because the variable with the same name which you use in your function is local, it is not visible outside the function. 这是因为您在函数中使用的具有相同名称的变量是本地的,在函数外部不可见。 To fix it use ticketSold = getTickets(alimit) . 要修复它,请使用ticketSold = getTickets(alimit) In general, it is a good idea to make functions return something if they are supposed to do some calculations, instead of using global variables: 一般来说,如果函数应该进行一些计算而不是使用全局变量,那么让函数返回一些东西是个好主意:

     def calcIncome(ticketSold,price): return price*ticketSold totalIncome = calcIncome(ticketSold,cPrice) print totalIncome 

Let's review your requirements piece by piece. 让我们一块一块地审查您的要求。

Use local named constants in the main module for seat costs and section seat limits that will be passed to the modules and functions described under the following points. 在主模块中使用本地命名常量来获取座位成本和部分座位限制,这些限制将传递给在以下几点下描述的模块和功能。

So there is a main module and it has local named constants. 所以有一个主模块,它有本地命名常量。 That's easy. 这很容易。

def income():
    SECTION_A = dict('name': 'A', 'seats': 100, 'price': 20)
    SECTION_B = dict('name': 'B', 'seats': 500, 'price': 15)
    SECTION_C = dict('name': 'C', 'seats': 200, 'price': 10)

Each constant is a dictionary with three slots. 每个常量都是一个包含三个插槽的字典。

Although there are three seating sections in the problem, a set of generic functions will be created that are used for any given section based upon the passed parameters. 虽然问题中有三个座位部分,但是将创建一组基于传递的参数用于任何给定部分的通用函数。 These functions will be called once for each section: A, B, and C. The general logic for these function is described below: 这些函数将针对每个部分调用一次:A,B和C.这些函数的一般逻辑如下所述:

1.)Include input function ( getTickets ) that takes section letter and seat limit for given section as parameters. 1.)包括输入函数( getTickets ),它将给定部分的分节字母和座位限制作为参数。 The number of tickets for the given section is returned. 返回给定部分的票数。 This function should only return a valid number of tickets; 此功能只应返回有效数量的票证; the validation function, ticketsValid , should be called from this function. 应该从此函数调用验证函数ticketsValid

The requirement basically dictates that the function must not return if ticketsValid returns false. 该要求基本上规定如果ticketsValid返回false,则函数不能返回。 I suppose that means we need to loop and ask again if not. 我想这意味着我们需要循环并再次询问。

    def getTickets(section_letter, seat_limit):
        while True:
            print "Tickets sold for section %s (max %d):" % (section_letter, seat_limit)
            reply = int(input())
            if ticketsValid(reply, seat_limit):
                return reply
            else:
                print "Sorry, try again."

This will fail if the input cannot be coerced into an int but that is probably acceptable for a first assignment. 如果输入无法强制转换为int ,则会失败,但第一次分配可能是可接受的。

2.) Include validation function ( ticketsValid ) that takes the tickets sold per section and seat limit per section as passed parameters. 2.)包括验证功能( ticketsValid ),它将每个部分销售的票证和每个部分的座位限制作为传递的参数。 A Boolean indicating whether the passed tickets sold for the given section is in the valid range is returned. 返回一个布尔值,指示为给定部分销售的传递的票证是否在有效范围内。 This validation function will be called from within the previously defined getTickets function. 将在先前定义的getTickets函数内调用此验证函数。

If I understand this correctly, "the valid range" means that we must not sell more than the seat limit, or a negative number. 如果我理解正确,“有效范围”意味着我们不得卖出超过座位限额或负数。

    def ticketsValid(number, seat_limit):
        if 0 <= number <= seat_limit:
            return True
        else:
            return False

It doesn't really make sense to put this in a separate function, IMHO, although the calling function might come out simpler by factoring this out. 把它放在一个单独的函数中是没有意义的,恕我直言,虽然调用函数可能通过分解出来更简单。

3.) Include income calculation function ( calcIncome ) that takes tickets sold and seat cost as passed parameters and returns the income generated for the section. 3.)包括收入计算函数( calcIncome ),它将售票和座位成本作为传递参数并返回为该部分生成的收入。

Again, this is so trivial as to be silly to put in a separate function, but if the calculation would change in the future (add VAT! Copy a percentage to the unemployment fund!) it might be good to have it in one place only. 再一次,这是琐碎的,愚蠢地放在一个单独的功能,但如果计算将来会改变(增加增值税!将一个百分比复制到失业基金!),将它放在一个地方可能是好的。

    def calcIncome(tickets_sold, seat_cost):
        return tickets_sold*seat_cost

What remains now for you is to connect these parts together to a meaningful whole. 现在剩下的就是将这些部分连接在一起,形成一个有意义的整体。 Take care to remember to assign the return value of every function to a variable! 注意记住将每个函数的返回值赋给变量!

    for section in [SECTION_A, SECTION_B, SECTION_C]:
        tickets_sold = getTickets(section['name'], section['seats'])
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM