【发布时间】:2018-10-04 21:55:05
【问题描述】:
我是一名使用 Python3.6 的初学者,我不知道为什么我的代码不能正常工作。非常感谢您的反馈/帮助。
我对DriveTo功能的说明如下:
现在,添加一个 Car 方法 driveTo。它应该采用两个附加参数,即汽车尝试移动的位置的 x 和 y 坐标。如果汽车有足够的汽油来行驶,则应该移动汽车,更新剩余的汽油量,并且该方法应该返回 True。如果汽车没有足够的油,根本不应该移动或改变它,并且该方法应该返回False
这是我当前的代码:
import math
class Car:
def __init__( self , mpg , fuel , money ):
self.mpg = mpg
self.fuel = fuel
self.money = money
#return current location of car in two element list
def getLocation( self ):
return [ self.x , self.y ]
#returns the number of gallons left in the car
def getGas( self ):
self.fuel -= 1
return self.fuel
#returns how much gas the car needs to be at capacity
def getToFill( self ):
current_tank = self.getGas()
gas_needed = ( self.fuel - current_tank )
return gas_needed
#return true or false if enough gas
def driveTo( self , x , y ):
self.x = x
self.y = y
miles_pg = (self.mpg / self.getToFill())
miles = math.sqrt( ( self.x - x )**2 + ( self.y - y )**2 )
if miles >= miles_pg:
return True
else:
return False
【问题讨论】:
-
你能补充一下你如何测试代码
-
你能指定哪一行出现错误吗?
-
将整个堆栈跟踪放在问题中。它向您(和我们)准确显示错误发生的位置,然后可以追溯这些值以查看错误的位置。
-
如果
self.fuel等于current_tank... -
我正在使用 Mac 的终端测试代码,错误特别是第 29 行“文件“car.py”,第 29 行,在 driveTo miles_pg = (self.mpg / self.getToFill()) "
标签: python class python-3.6