Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取名称错误NameError,函数未定义?_Python_Python 3.x_Import_Nameerror - Fatal编程技术网

Python 获取名称错误NameError,函数未定义?

Python 获取名称错误NameError,函数未定义?,python,python-3.x,import,nameerror,Python,Python 3.x,Import,Nameerror,文件“/Users//Desktop/A5/driverModule.py”,第22行,在控制器中 tripInfo() name错误:未定义名称“tripInfo” 好吧,我真的被卡住了,不明白为什么会出现这个错误。感谢您的帮助 驱动模块 from TripInfoModule import * def controller(): totalDrivingTime = 0 totalFuelCost = 0 totalAmountEarned = 0 totalNetIncome = 0

文件“/Users//Desktop/A5/driverModule.py”,第22行,在控制器中 tripInfo() name错误:未定义名称“tripInfo”

好吧,我真的被卡住了,不明白为什么会出现这个错误。感谢您的帮助

驱动模块

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()
单元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
单元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

这只是不鼓励导入的原因之一。从这里明确开始

from TripInfoModule import tripInfo, ...
如果那一行找不到tripInfo,那么你就更快地接近这个bug了

现在
tripInfo
在您称之为“Module2”的东西中定义


这个文件当然需要命名为“TripInfoModule”。确保没有可以加载的旧版本或
.pyc
文件,而不是您期望的文件

驱动程序模块中是否有
导入模块2
?是。这就是我不明白的。我正确导入了它,但它仍然给出了一个名称错误尝试此
打印(文件名\u.tripInfo())
它返回什么?您正在使用的文件名是什么?我设计了3个模块。第一个模块获取用户输入,第二个模块进行一些计算并获取一些总数。最后一个模块是运行整个程序的驱动程序。我在第二个模块上遇到了一个问题,我发现了这个错误:ImportError:无法导入名称“tripInfo”@WonderphuL,是的,这告诉你Python在
TripInfoModule.py
中找不到
tripInfo
,所以这就是你需要解决的问题好吧,这就是我所困惑的。为什么它找不到?因为它不在那里。但你也知道这不是一个语法错误或其他问题。没有看到实际的文件,我无法告诉你更多。再次检查它是否正在加载您认为正确的文件。