1

コードを修正する方法についての指針はありますか? このコードは、販売されたチケットの数についてユーザー入力を求め、関数を使用して生成された収入を返します。

各関数の呼び出し方がわからない

secA = 20
secB = 15
secC = 10

def main():
    print("The income generated from all sections is: ", total)
def getTickets(A,B,C):
    sectionA = int(input("Please enter the number of tickets sold in section A: ")
    sectionB = int(input("Please enter the number of tickets sold in section B: ")
    sectionC = int(input("Please enter the number of tickets sold in section C: ")

    def ticketsValid():
        while sectionA > 300:
                print("ERROR: Section A has a limit of 300 seats")
        while sectionB > 500:
                print("ERROR: Section B has a limit of 500 seats")
        while sectionC > 200:
                print("ERROR: Section C has a limit of 200 seats")

    def calcIncome():
        total = secA * sectionA + secB * sectionB + secC * sectionC
        print("The income generated is $", format(total, '.2f'))   
main()
4

2 に答える 2

3

最初の質問に答えるには、関数の名前を関数に入れる必要があるすべての関数を呼び出す必要がありますmain()。しかし、他にもいくつかのエラーがあったため、プログラムを順を追って説明することにしました。

まず、価格を設定します。

secA = 20
secB = 15
secC = 10

これが最初の関数です。getTickets()

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: "))

global変数を使用する前に単語に注意してください。これは、この変数がどこでも使用できることをコンピューターに伝えます。次に、二重括弧に注意してください。int()とはどちらinput()も関数であるため、それを行うことでそれを示す必要があります。

ticketsValid()関数のコードを修正しました。通常、関数をネストすることはお勧めできないため、これは上記のコードと同じインデント レベルです。

def ticketsValid(A,B,C):
    while A > 300 or A < 0:
        print("ERROR: Section A has a limit of 300 seats\n")
        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: "))

これは上記から変数を取得し、それらが有効かどうかを確認します。負の数のチェックを追加したことに注意してください。負のチケットは販売できません。

次に、次のようになりcalcIncome(A,B,C)ます。

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

まず、セクションに設定価格を掛けて合計を計算します。次に、それを印刷します。

最後に、関数を呼び出す必要があります。他の関数を使用するmain()関数にあなたのアイデアを使用しました。このように見えます。

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

実行時に、他の関数を正しい順序で呼び出すだけです。

最後に、次のようにmain()入力して関数を呼び出します。

main()

これがあなたの質問に答えたことを願っています。そうでない場合は、お気軽にコメントしてください。もしそうなら、私の答えの横にある緑色のチェックマークをチェックしてください。

于 2013-03-10T21:37:00.873 に答える
1

ネストされた関数の使用方法のみを知りたい場合:

def f():
    def g(): #this defines a function but does not call it
        print "run g"
    g() # this calls g.

通常、ネストされた関数は、その親関数の外では使用できません。ネストされた関数を使用するポイントは、その関数がその親関数が何かを行うのを助けるだけだからです。外したい場合は、新しい関数として定義することを検討してください。

あなたの場合、コードをパーツに分割する方法を検討する必要があります。
私があなたなら、getTickets() を使用してチケットを取得します。
ticketValid は問題ありませんが、ブール値を返すようにします。calcIncome は総収入を返します。したがって、一般的な設計は次のようになります。

def main():
    (A, B, C) = getTickets()
    if(ticketsValid(A, B, C)):
        income = calcIncome(A, B, C)
        print("The income generated from all sections is: ", income)

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

こっちの方がいいデザインだと思います。

于 2013-03-10T21:57:11.933 に答える