0

私は初めてpythonを使用していますが、この悪臭の問題に悩まされており、なぜそれが機能しないのかを一生理解できません。プログラムを実行しようとすると、変更なしで年間コストの答えを得ることができますが (間違っていて理由がわかりません)、変更を加えた場合の年間コストは得られません。

コロン/括弧/ectを逃した場合に備えて書き直そうとしましたが、うまくいきませんでした。名前を変更してみました。そして、私はそれを完全に取り除いてみました(これが、その迷惑なエラーメッセージを取り除くことができる唯一の方法です)

ペイオフファイル

from mpg import *

def main():
    driven,costg,costm,mpgbm,mpgam = getInfo(1,2,3,4,5)
    print("The number of miles driven in a year is",driven)
    print("The cost of gas is",costg)
    print("The cost of the modification is",costm)
    print("The MPG of the car before the modification is",mpgbm)
    print("The MPG of the car afrer the modification is",mpgam)

costWithout = getYearlyCost(1,2)
print("Yearly cost without the modification:", costWithout)

costWith = getYearlyCost2()
print("Yearly cost with the modification:", costWith)

これにはエラー(おそらく多くのエラー)があることはわかっていますが、それを見ることはできません。誰かが私にそれを指摘して、私がそれを修正するのを手伝ってくれませんか?

また、ペイオフ ファイルではなくエラーが発生した場合に備えて、mpg.py を追加しました。

def getInfo(driven,costg,costm,mpgbm,mpgam):
    driven = eval(input("enter number of miles driven per year: "))
    costg = eval(input("enter cost of a gallon of gas: "))
    costm = eval(input("enter the cost of modification: "))
    mpgbm = eval(input("eneter MPG of the car before the modification: "))
    mpgam = eval(input("enter MPG of the car after the modification: "))
    return driven,costg,costm,mpgbm,mpgam

def getYearlyCost(driven,costg):
    getYearlyCost = (driven / costg*12)
def getYealyCost2(driven,costm):
    getYearlyCost2 = (driven / costm*12)
    return getYearlyCost,getYearlyCost2

def gallons(x,y,z,x2,y2,z2):
    x = (driven/mpgbm)     # x= how many gallons are used in a year
    y = costg
    z = (x*y)               # z = how much money is spent on gas in year
    print("money spent on gas in year ",z)

    x2 = (driven/mpgam)     # x2 = how much money is spent with mod.
    z2 = (x2*y)
    y2 = (costm + z2)
                                                          1,1           Top
4

1 に答える 1

4

差し迫った問題は次のとおりです。

costWith = getYearlyCost2()

呼び出そうとしている関数には名前が付けられていますgetYealyCost2()("r" はありません)。

それを修正するとすぐに見つかる他の問題があります。たとえば、returnステートメントがない、関数をgetYearlyCost()返そうとする、引数なしで呼び出すなどです。getYearlyCost()getYearlyCost2()getYearlyCost2()

その上、import *眉をひそめ、次にeval()... の使用がありますが、それで十分です。

于 2012-02-24T06:40:34.753 に答える