Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
don';我不知道我的错误是什么(python)_Python_Python 2.7 - Fatal编程技术网

don';我不知道我的错误是什么(python)

don';我不知道我的错误是什么(python),python,python-2.7,Python,Python 2.7,实现具有以下属性的类Car。一辆汽车有一个轮子 一定的燃油效率(以英里/加仑为单位)和一定的 油箱中的燃油量。效率在 构造函数,初始燃油油位为0。提供一种驱动方法 模拟驾驶汽车行驶一定距离,减少 油箱中的燃油油位和返回油箱的方法getGasLevel 当前燃油油位和添加的气体,以加满油箱。示例用法: myHybrid = Car(50) myHybrid.addGas(20) myHybrid.drive(100) print(myHybrid.getGasLevel()) 编写一个主程序来测

实现具有以下属性的类Car。一辆汽车有一个轮子 一定的燃油效率(以英里/加仑为单位)和一定的 油箱中的燃油量。效率在 构造函数,初始燃油油位为0。提供一种驱动方法 模拟驾驶汽车行驶一定距离,减少 油箱中的燃油油位和返回油箱的方法getGasLevel 当前燃油油位和添加的气体,以加满油箱。示例用法:

myHybrid = Car(50)
myHybrid.addGas(20)
myHybrid.drive(100)
print(myHybrid.getGasLevel())
编写一个主程序来测试你的类

我的代码:

class Car(object):
    def __init__(self, fuelEfficiency):
        super(Car, self).__init__()
        self.fuelEfficiency = fuelEfficiency
        self.fuelLevel = 0  #   In gallons


    #   Returns the amount of gas
    def getGasLevel(self):
        return self.fuelLevel

    #   Adds gas
    def addGas(self, gasToAdd):
        self.fuelLevel += gasToAdd

    #   Simulates driving car for a given distance
    #   and reduces the amount of gas based on the
    #   fuelEfficiency.
    def drive(self, distanceInMiles):
        gasToRemove = self.fuelEfficiency * distanceInMiles

        self.fuelLevel -= gasToRemove

        #   Ensure we don't go below zero gas
        self.fuelLevel = max(self.fuelLevel,0)


def main():

    myHybrid=Car(50)
    myHybrid.addGas(20)
    myHybrid.drive(100)
    print(myHybrid.getGasLevel())

实际上,您需要调用
main()

在底部添加以下行(无缩进):

有几件事:

  • 您不调用
    main()
    。我怀疑这是一个复制粘贴错误

  • 你因为减少燃料而把数学搞砸了。而不是
    self.fuelEfficiency*distanceInMiles
    它应该是
    distanceInMiles/self.fuelEfficiency
    。您当前的代码是减去100*50加仑,而不是12加仑。因此,很明显,您的燃油将变为0

  • 新代码
    你为什么认为有“错误”?所发生的事情并不是你所期望的?为什么你要把它同时标记为和?不可能两者都是。对不起,我是一个完全的初学者,所以我只是尝试在两个版本上都标记它。AsongToruin,我没有得到任何类型的值。你确定是你编写的代码吗?如果你这么做了,你会惊讶地发现你和这些家伙有着完全相同的间隔和评论。。。您的代码正在工作,它输出
    0
    。它确实做了它应该做的,但正如另一个答案所指出的,你在数学上犯了错误。
    if __name__ == '__main__': 
        main()
    
    class Car(object):
        def __init__(self, fuelEfficiency):
            super(Car, self).__init__()
            self.fuelEfficiency = fuelEfficiency
            self.fuelLevel = 0  #   In gallons
    
        #   Returns the amount of gas
        def getGasLevel(self):
            return self.fuelLevel
    
        #   Adds gas
        def addGas(self, gasToAdd):
            self.fuelLevel += gasToAdd
    
        #   Simulates driving car for a given distance
        #   and reduces the amount of gas based on the
        #   fuelEfficiency.
        def drive(self, distanceInMiles):
            gasToRemove = distanceInMiles / self.fuelEfficiency
            self.fuelLevel -= gasToRemove
            #   Ensure we don't go below zero gas
            self.fuelLevel = max(self.fuelLevel,0)
    
    
    def main():
        myHybrid=Car(50)
        myHybrid.addGas(20)
        myHybrid.drive(100)
        print(myHybrid.getGasLevel())
    
    if __name__ == '__main__': 
        main()