简体   繁体   English

出现名称错误 NameError,函数未定义?

[英]Getting a name error NameError, function is not defined?

File "/Users//Desktop/A5/driverModule.py", line 22, in controller tripInfo() NameError: name 'tripInfo' is not defined文件“/Users//Desktop/A5/driverModule.py”,第 22 行,控制器 tripInfo() NameError: name 'tripInfo' is not defined

Okay I'm really stuck and can't figure out why I'm getting this error.好的,我真的被卡住了,无法弄清楚为什么我会收到这个错误。 Any help is appreciated.任何帮助表示赞赏。

Driver Module驱动模块

from TripInfoModule import *


def controller():
totalDrivingTime = 0
totalFuelCost = 0
totalAmountEarned = 0
totalNetIncome = 0
totalDistance = 0
totalFuelComsumed = 0
totalBreakTime = 0

input("Welcome to the updated trip income calculator. Press enter to continue: ")
i = "y"
while i == "y":
    getDistance()
    getEarningpPerMile()
    getDrivingTime()
    getBreakTime()
    getFuelConsumed()
    getCostperGallon()
    tripInfo()

    i = input("Would you like to add a new trip? Enter y to add a new trip, "
          "or enter n to exit.")
print("Total fuel cost: $", format(totalFuelCost, '.2f'))
print("Total amount of fuel consumed:", format(totalFuelComsumed, '.2f'), "gallons")
print("Total miles driven:", format(totalDistance, '.2f'), "miles")
print("Total driving time:", format(totalDrivingTime, '.2f'), "hours")
print("Total break time:", format(totalBreakTime, '.2f'), "hours")
print("Total earned: $", format(totalAmountEarned, '.2f',))
print("Total net income: $", format(totalNetIncome, '.2f'))


def main():
controller()


main()

Module 2模块 2

from userInputModule import *

import driverModule

def tripInfo():
getNetIncome()
getTotalFuelConsumed()
getAmountEarned()
getNetIncome()
getTotalBreakTime()
getTotalDistance()
getTotalFuelConsumed()
getTotalDrivingTime()

def getFuelCost():
fuelConsumed = getFuelConsumed()
costPerGallon = getCostperGallon()
totalFuelCost = driverModule.controller()

gasUsed = float(fuelConsumed)
costPerGal = float(costPerGallon)

fuelCost = gasUsed * costPerGal
totalFuelCost += fuelCost

print("Your total fuel cost is:", format(fuelCost, '.2f'))

return fuelCost, totalFuelCost


def getAmountEarned():
distance = getDistance()
earnings = getEarningpPerMile()
totalAmountEarned = driverModule.controller()

distanceVar = float(distance)
earningsVar = float(earnings)

amountEarned = distanceVar * earningsVar
totalAmountEarned += amountEarned

print("Your earnings are:", format(amountEarned, '.2f'))

return amountEarned, totalAmountEarned


def getNetIncome():
totalNetIncome = driverModule.controller()
fuelConsumed = getFuelConsumed()
costPerGallon = getCostperGallon()
distance = getDistance()
earnings = getEarningpPerMile()

gasUsed = float(fuelConsumed)
costPerGal = float(costPerGallon)

distanceVar = float(distance)
earningsVar = float(earnings)

earnedIncome = distanceVar * earningsVar

fuelCost = gasUsed * costPerGal
taxAmount = earnedIncome * .08
netIncome = (earnedIncome - taxAmount) - fuelCost

totalNetIncome += netIncome

print("Your net income is:", format(netIncome, '.2f'))

return netIncome


def getTotalBreakTime():
breakTime = getBreakTime()
totalBreakTime = driverModule.controller()

breakTimeVar = float(breakTime)
totalBreakTime += breakTimeVar

return totalBreakTime


def getTotalDistance():
distance = getDistance()
totalDistance = driverModule.controller()

distanceVar = float(distance)
totalDistance += distanceVar

return totalDistance


def getTotalFuelConsumed():
fuelConsumed = getFuelConsumed()
totalFuelConsumed = driverModule.controller()

fuelConsumedVar = float(fuelConsumed)
totalFuelConsumed += fuelConsumedVar

return totalFuelConsumed


def getTotalDrivingTime():
drivingTime = getDrivingTime()
totalDrivingTime = driverModule.controller()

drivingTimeVar = float(drivingTime)
totalDrivingTime += drivingTimeVar

return totalDrivingTime

Module 1模块 1

def getDistance():
distance = input("Please enter the number of miles driven: ")
distanceStr = distance.replace(".", "")

while not distanceStr.isnumeric():
    distance = input("Please enter a numeric distance "
                           "only that is non-zero: ")
    distanceStr = distance.replace(".", "")

return distance


def getEarningpPerMile():
earningsPerMile = input("Please enter the amount earned per mile: ")
earningsPerMileStr = earningsPerMile.replace(".", "")

while not earningsPerMileStr.isnumeric():
    earningsPerMile = input("Please enter a numeric number "
                           "only that is non-zero: ")
    earningsPerMileStr = earningsPerMile.replace(".", "")

return earningsPerMile


def getDrivingTime():
totalDrivingTime = input("Please enter total driving time: ")
totalDrivingTimeStr = totalDrivingTime.replace(".", "")

while not totalDrivingTimeStr.isnumeric():
    totalDrivingTime = input("Please enter a numeric number "
                           "only that is non-zero: ")
    totalDrivingTimeStr = totalDrivingTime.replace(".", "")

return totalDrivingTime


def getBreakTime():
breakTime = input("Please enter the amount of break time taken: ")
breakTimeStr = breakTime.replace(".", "")

while not breakTimeStr.isnumeric():
    breakTime = input("Please enter a numeric number "
                           "only that is non-zero: ")
    breakTimeStr = breakTime.replace(".", "")

return breakTime


def getFuelConsumed():
fuelConsumed = input("Please enter the ammount of fuel consumed: ")
fuelConsumedStr = fuelConsumed.replace(".", "")

while not fuelConsumedStr.isnumeric():
    fuelConsumed = input("Please enter a numeric number "
                           "only that is non-zero: ")
    fuelConsumedStr = fuelConsumed.replace(".", "")

return fuelConsumed

def getCostperGallon():
costPerGallon = input("Please enter cost per gallon: ")
costPerGallonStr = costPerGallon.replace(".", "")

while not costPerGallonStr.isnumeric():
    costPerGallon = input("Please enter a numeric number "
                           "only that is non-zero: ")
    costPerGallonStr = costPerGallon.replace(".", "")

return costPerGallon

This is just one of the reasons that * imports are discouraged.这只是不鼓励*导入的原因之一。 Start by being explicit here从这里明确开始

from TripInfoModule import tripInfo, ...

If that line can't find tripInfo , then you have got closer to the bug sooner.如果该行找不到tripInfo ,那么您已经更快地接近了这个错误。

Now tripInfo is defined in the thing you call "Module2"现在tripInfo在你称之为“Module2”的东西中定义

This file of course needs to be named "TripInfoModule".这个文件当然需要命名为“TripInfoModule”。 Make sure there are no old versions or .pyc files that could be loaded instead of the one you are expecting确保没有可以加载的旧版本或.pyc文件,而不是您期望的文件

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

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