I am not quite sure how to describe this problem so i will just say what it's supposed to do and what it is actually doing.
what's supposed to happen in the below code is when i run car_name.drive()
if random.choice(EVENTS) == "gas_station":
AT_GAS_STATION = True
print("You are at a gas station")
and if AT_GAS_STATION is true is prints'you are at a gas station' which it does, but if i then run car_name.refuel it should execute this part:
elif self.fuel_level < self.fuel_capacity and AT_GAS_STATION == True:
self.fuel_level = self.fuel_capacity
print("Your fuel tank is full")
But it does not it instead executes this:
elif self.fuel_level < self.fuel_capacity and AT_GAS_STATION == False:
print("You are not at a gas station and therfore can't fill up")
what i mean is that an idle session would look like this:
>>> from car import Car
>>> pilot = Car("honda", "pilot", 2003, 19, "blue")
>>> pilot.drive()
You are at a gas station
The car is moving
>>> pilot.refuel()
You are not at a gas station and therfore can't fill up
So i believe that the problem is it is not changing AT_GAS_STATION to False
I have tried running getattr(pilot, "self.fuel_level") to see if i had filled up but just printed the wrong message but i got this:
Traceback (most recent call last):
File "<pyshell#80>", line 1, in <module>
getattr(pilot, "self.fuel_level")
AttributeError: 'Car' object has no attribute 'self.fuel_level'
The full code:
import random
EVENTS = [
"gas_station",
"nothing",
"nothing",
"nothing",
"nothing",
"nothing"]
random.shuffle(EVENTS)
AT_GAS_STATION = False
class Car():
"""Your car."""
def __init__(self, make, model, year, fuel_capacity, color=None):
"""Atributtes of your car, fuel in gallons."""
self.color = color
self.make = make
self.model = model
self.year = year
self.fuel_capacity = fuel_capacity
self.fuel_level = self.fuel_capacity
if self.year < 2000:
print("Your car is old")
def refuel(self):
"""Fill up your gas."""
if self.fuel_level >= self.fuel_capacity:
print("Your tank is already full :P")
elif self.fuel_level < self.fuel_capacity and AT_GAS_STATION == False:
print("You are not at a gas station and therfore can't fill up")
elif self.fuel_level < self.fuel_capacity and AT_GAS_STATION == True:
self.fuel_level = self.fuel_capacity
print("Your fuel tank is full")
def drive(self):
"""Drive your car"""
self.fuel_level = self.fuel_level -1
if random.choice(EVENTS) == "gas_station":
AT_GAS_STATION = True
print("You are at a gas station")
elif random.choice(EVENTS) != "gas_station":
AT_GAS_STATION = False
if self.fuel_level < 1:
print("You need more gas")
elif self.fuel_level <= self.fuel_capacity /2:
print(self.fuel_level -1)
else:
print("The car is moving")
def specs(self):
"""Display specs of your car"""
print(self.color, self.year, self.make, self.model, self.fuel_capacity, "Gallons")
Hopefully i have provided enough information but if you want more just ask.
Aucun commentaire:
Enregistrer un commentaire