1

大家好,我遇到的问题是电影院门票和部分。基本上,该程序应该接受输入的门票并产生总收入。一个要求是如果输入的门票数量超过该部分的座位限制,则执行无效功能。该程序运行良好,但当发生失效并输入适当数量的票时,它将计算导致失效功能执行的票,以及新售出的适当数量的票。请帮忙。谢谢!

    secA = 20
    secB = 15
    secC = 10
    def getTickets():

        global A
        A = int(input("Please enter the number of tickets sold in section A: "))

        global B
        B = int(input("Please enter the number of tickets sold in section B: "))

        global C
        C = int(input("Please enter the number of tickets sold in section C: "))

    def ticketsValid(A,B,C):
        while A > 300 or A < 0:
            print("ERROR: Section A has a limit of 300 seats")
            A = int(input("Please enter the number of tickets sold in section A: "))
        while B > 500 or B < 0:
            print("ERROR: Section B has a limit of 500 seats")
            B = int(input("Please enter the number of tickets sold in section B: "))
        while C > 200 or C < 0:
            print("ERROR: Section C has a limit of 200 seats")
            C = int(input("Please enter the number of tickets sold in section C: "))

    def calcIncome(A, B, C):
        incomeGenerated = A * secA + B * secB + C * secC
        print("The income generated is $%d" % (incomeGenerated))

    def main():
        getTickets()
        ticketsValid(A,B,C)
        calcIncome(A, B, C)

    main()
4

2 回答 2

1

问题是在你内部ticketsValid只是改变局部变量A和。因此,当您离开该函数时,所有更改的值都会丢失,并且会再次使用全局空间中的值。BC

global A, B, C您可以像在函数中那样再次调用来解决此问题getTickets,或者更好的解决方案是让函数返回值。所以getTickets将返回一个元组(A, B, C)ticketsValid将获取该元组并再次返回这样一个元组(带有修改后的值),calcIncome然后再次使用。

例如像这样:

def getTickets():
    A = int(input("Please enter the number of tickets sold in section A: "))
    B = int(input("Please enter the number of tickets sold in section B: "))
    C = int(input("Please enter the number of tickets sold in section C: "))
    return (A, B, C)

def ticketsValid(A, B, C):
    while A > 300 or A < 0:
        print("ERROR: Section A has a limit of 300 seats")
        A = int(input("Please enter the number of tickets sold in section A: "))
    while B > 500 or B < 0:
        print("ERROR: Section B has a limit of 500 seats")
        B = int(input("Please enter the number of tickets sold in section B: "))
    while C > 200 or C < 0:
        print("ERROR: Section C has a limit of 200 seats")
        C = int(input("Please enter the number of tickets sold in section C: "))
    return (A, B, C)

def calcIncome(A, B, C):
    incomeGenerated = A * secA + B * secB + C * secC
    print("The income generated is $%d" % (incomeGenerated))

def main():
    A, B, C = getTickets()
    A, B, C = ticketsValid(A, B, C)
    calcIncome(A, B, C)
于 2013-03-11T19:08:44.997 回答
0

您在函数头 def TicketsValid(A,B,C): 中使用了与全局变量相同的变量名称。因此,当您在 ticketValid 中覆盖 A、B 或 C 时,您只是在本地覆盖它,而不是全局变量。您需要在函数中使用 globals()['A'] 。

def ticketsValid(A,B,C):
    while A > 300 or A < 0:
        print("ERROR: Section A has a limit of 300 seats")
        globals()['A'] = int(input("Please enter the number of tickets sold in section A: "))
    while B > 500 or B < 0:
        print("ERROR: Section B has a limit of 500 seats")
        globals()['B'] = int(input("Please enter the number of tickets sold in section B: "))
    while C > 200 or C < 0:
        print("ERROR: Section C has a limit of 200 seats")
        globals()['C'] = int(input("Please enter the number of tickets sold in section C: "))
于 2013-03-11T19:09:19.530 回答