specialist/car.py

58 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Car():
# Пробег
kilometer = 0
def fill(self, add_gas):
total = self.gas + add_gas
if total <= self.capacity:
self.gas += add_gas
print('залито', add_gas, 'топлива\n')
if total > self.capacity:
self.gas += (add_gas - (total - self.capacity))
print('залито до полного бака:', add_gas - (total - self.capacity), ',', total - self.capacity, '- лишние\n')
def ride(self, expense):
# Сколько можем проехать из желаемого
possible_drive = self.gas / self.per_km
# Сколко едем по факту
if expense < possible_drive:
fact_drive = expense
else: fact_drive = possible_drive
if fact_drive < expense:
print('Можем проехать только', fact_drive, 'км')
self.gas -= (fact_drive * self.per_km)
self.kilometer += fact_drive
self.per_km += ((self.per_km * 0.01 * fact_drive) / 100)
print('Проехано:', fact_drive, 'км\n')
def __init__(self, gas, capacity, per_km):
self.capacity = capacity
self.gas = gas
self.per_km = per_km
def __str__(self):
return 'Вместимость бака: ' + str(self.capacity) + '\nТоплива в баке: ' + str(self.gas) + '\nРасход на километр: ' + str(self.per_km) + '\nПробег:' + str(self.kilometer) +'\n'
c1 = Car(2, 40, 0.1)
print('ааартуем\n\n', c1, sep='')
while True:
expense = input('Сколько желаем проехать: \n (n - exit)')
if expense == 'n':
break
else:
c1.ride(int(expense))
if c1.gas <= 0:
print('Топливо закончилось!')
print('Залить до полного? y/n')
if input() == 'y':
c1.fill(c1.capacity)
else: break
print(c1)