python变量没有在函数中更新

时间:2017-06-16 19:21:34

标签: python class oop methods

class car():
    def __init__(self,make,model,year,fuel_capacity,perkilometer):
        self.make = make
        self.model = model
        self.year = year
        self.fuel_capacity = fuel_capacity
        self.perkilometer = perkilometer

        self.fuel_level = 0

    def fill_tank(self):
        """fills the tank full capacity"""
        self.fuel_level = self.fuel_capacity
        print 'Tank is Full'

    def drive(self):
        """drives untill out of gas"""
        print 'car is moving'
        i = 0

        while 0 <= self.fuel_level:

            self.fuel_level -= self.perkilometer
            print '%s kilometres' % (self.fuel_level-(self.fuel_level-i))
            i+=1

        print 'out of gas'

    def travel(self,kilometer):
        """travels for distance given"""
        self.kilometer = kilometer
        print 'Car is moving'
        i=0

        while 0!=kilometer:
            kilometer-=1

            i+=1

            if self.fuel_level >= self.perkilometer:
                self.fuel_level -= self.perkilometer
                print '%s kilometres' % (kilometer - (kilometer - i))

            elif self.fuel_level < self.perkilometer:
                self.fuel_level=0
                print 'Out of Gas'
                return 0

            elif self.fuel_level == 0:
                print 'Out of Gas'
                return 0

        print 'Reached to destination'

    def continue_driving(self):
        distance_left = self.kilometer - self.fuel_level / self.perkilometer
        leters_needed = distance_left * self.perkilometer

        if distance_left<=self.fuel_level/self.perkilometer:
            print 'Have enough gas to continue'

        else:
            print 'Need %s leter more'% leters_needed


    def update_fuel_level(self,new_level):
        if  new_level <= self.fuel_capacity:
            self.fuel_level = new_level
            print 'Tank has %s leters' % self.fuel_level
        else:
            print "Tank can't hold that much"


    def add_fuel(self,amount):
        """adds fuel to gas tank"""

        if self.fuel_level+amount<=self.fuel_capacity:
            self.fuel_level+=amount
            print 'Added fuel %s leters '%amount

        else:
            print "Tank won't hold that much"

#somehow fuel level is not updating after or in add_fuel func.

my_car = car('hyundai','i20','2016','45',0.083)
my_car.fuel_level = 2
my_car.travel(50)
my_car.add_fuel(1)
my_car.continue_driving()

输出:

Car is moving
1 kilometres
2 kilometres
3 kilometres
4 kilometres
5 kilometres
6 kilometres
7 kilometres
8 kilometres
9 kilometres
10 kilometres
11 kilometres
12 kilometres
13 kilometres
14 kilometres
15 kilometres
16 kilometres
17 kilometres
18 kilometres
19 kilometres
20 kilometres
21 kilometres
22 kilometres
23 kilometres
24 kilometres
Out of Gas
Added fuel 1 leters 
Need 3.15 leter more

我在python中制作了这款车但是当我使用continue_driving方法时,似乎fuel_level无法更新无法找到错误。

我想要实现的主要目的是计算如果没有给坦克添加燃料,坦克中的气体是否足够或者没有。

正如您所看到的,当我运行此代码时,它说我们需要3.15升,但它应该是8.3 / 2 - (1 + 2)= 2.15

1 个答案:

答案 0 :(得分:0)

您没有跟踪行进的距离。

你试图根据你消耗的燃料和每公里的升数来计算你的距离。

distance_left = self.kilometer - self.fuel_level / self.perkilometer

在使用self.fuel_level计算distance_left之前,您的代码正在增加my_car.add_fuel(1) # Increased self.fuel_level my_car.continue_driving() # Use self.fuel_level to determine distance_left

def __init__(self,make,model,year,fuel_capacity,perkilometer):
    self.make = make
    self.model = model
    self.year = year
    self.fuel_capacity = fuel_capacity
    self.perkilometer = perkilometer

    self.fuel_level = 0
    self.distance_traveled = 0

我建议您跟踪行进的距离。

if self.fuel_level >= self.perkilometer:
    self.fuel_level -= self.perkilometer
    self.distance_traveled += 1
    print '%s kilometres' % self.distance_traveled

然后每次旅行时递增

main