1
def main():
    userInput()
    calculate()

def userInput():
    print("Please put in the weight of your package:")
    a= input()
    weight= float(a)


def calculate():
    if weight <= 2:
        print('Your rate is $1.10')
    elif weight > 2 or weight <= 6:
        print('Your rate is $2.20')
    elif weight > 6 or weight <= 10:
        print('Your rate is $3.70')
    else:
        print('Your rate is $3.80')
main()

基本的に、「userInput」モジュールのデータを「calculate」モジュールでどのように使用できるか疑問に思っていました。私は議論をパスすることを知っていますが、(そしてこれは私を狂気に駆り立てています)私の人生では、それを行う適切な方法を理解できません。引数の概念は理解していますが、実際にコードに実装することはできません。ありがとう。

4

2 に答える 2

0

weightパラメータとして渡すことができます

def userInput():
    a= input("Please put in the weight of your package:")
    weight = None
    try:
        weight= float(a)
    except:
        userInput()
    return weight

def calculate(weight):
    if weight <= 2:
        print('Your rate is $1.10')
    elif weight > 2 and weight <= 6:
        print('Your rate is $2.20')
    elif weight > 6 and weight <= 10:
        print('Your rate is $3.70')
    else:
        print('Your rate is $3.80')

def main():
    weight = userInput()
    calculate(weight)

main()
于 2013-09-30T19:53:22.347 に答える
0

余談ですが、重みチェックを次のように書き直すことができます。

if weight <= 2:
    print('Your rate is $1.10')
elif 2 < weight <= 6:
    print('Your rate is $2.20')
elif 6 < weight <= 10:
    print('Your rate is $3.70')
else:
    print('Your rate is $3.80')

「n < x < n+」表記の使用に注意してください

shantanoo からのコメント後の更新:

元の質問のコードを見ていました:

def calculate():
    if weight <= 2:
        print('Your rate is $1.10')
    elif weight > 2 or weight <= 6:
        print('Your rate is $2.20')
    elif weight > 6 or weight <= 10:
        print('Your rate is $3.70')
    else:
        print('Your rate is $3.80')

そして、2 つの elif 行では、最初の比較は必要なく、コードを次のように書き直すことができることに気付きました。

def calculate():
    if weight <= 2:
        print('Your rate is $1.10')
    elif weight <= 6:
        print('Your rate is $2.20')
    elif weight <= 10:
        print('Your rate is $3.70')
    else:
        print('Your rate is $3.80')
于 2013-09-30T20:08:41.883 に答える