当前位置:网站首页>Fuel consumption calculator

Fuel consumption calculator

2022-07-05 04:06:00 Fantasy elves_ cq



Fuel consumption calculator


  

  1. subject
  2. Code running effect
  3. Complete code

Exercise questions

 Insert picture description here


Home

My code running effect



Home

My problem solving code

( If you can't make clear the function from the statement annotation , Please leave a message in the comment area for advice and discussion .)
# coding: utf-8


'''  Please pay attention to   Walrus operator is used in fuel consumption calculation expression  := (3.9+ python Version specific operators ), Such as expression code   Please do not make any changes in  Python 3.9+  Environment is running .'''



def error():
    s = '''  Error message printing  '''
    print(f'\n\n\n{
    s:=^36}\n\n\
{
    " Input error ! Please try again .":^33}\n\n\
{
    "":*^42}\n')



s = '''  The state of two refuels must be consistent ,  To accurately calculate the fuel consumption .  Last time “ Fill it up with ” perhaps “ The oil gauge lights up ” when ,  Record the mileage , Wait until again “ Fill it up with ” perhaps  “ The oil gauge lights up ” when , The interval can be calculated accurately   Fuel consumption .  The last time “ The oil gauge lights up ” Fuel filling volume at , namely   It was last time “ The oil gauge lights up ” This time “ The oil gauge lights up ”  Mileage used ; This time, “ Fill it up with ” Refueling volume of ,  Last time “ Fill it up with ” Here we are “ Fill it up with ” Mileage   Use oil .'''

print(f'\n\n\n{"  Fuel consumption calculation  ":_^38}')
s = '1.  Fill the tank ', '2.  The oil gauge lights up '

while True:
    print(f'\n{"":.^42}\n\n{" ".join(s):^34}\n\n{".":.>42}')
    k = input(f'{" Please select the fuel tank status :":>12}')
    if k not in ('1', '2'):
       error()
    else:
        k = s[k := int(k) - 1][3:]
        break
    
# Enter mileage 
milage_last, milage_local = map(float, (input(f'\n{s:>10}:') for s in (f" The last time {k} The mileage ", f' This time, {k} The mileage ')))
# Enter the oil price and refueling amount 
oil_price, sum_of_money = map(float, (input(f'\n{s:>10}:') for s in (" Oil price in the service station ", ' Refueling amount ')))
# Calculate fuel consumption ( Use of the  python 3.9+  Walrus operators supported )
oil_wear = ((sum_oil := sum_of_money / oil_price)\
 / (sum_milage := milage_local - milage_last)\
) * 100

print(f'\n\n\n{
    " Conditions for accurate fuel consumption calculation ":.^33}\
\n\n{
    s}\
\n\n{
    " Conditions for accurate fuel consumption calculation ":.^33}')

print(f'\n\n\n{
    " Fuel consumption calculator ":_^37}\
\n\n\n{
    " Last mileage :":>15}{
    milage_last} km\
\n{
    " Current mileage :":>15}{
    milage_local} km\
\n{
    " Refueling status :":>15}{
    k}\
\n{
    " Fuel consumption mileage :":>15}{
    sum_milage} km\n\
\n{
    "Cut-off Rule":.^42}\n\
\n{
    " Current oil prices :":>15}{
    oil_price:.2f}  element / l \
\n{
    " Refueling amount :":>15}{
    sum_of_money:.2f}  element \
\n{
    " Add   oil   The amount :":>16}{
    sum_oil:.2f}  l \n\
\n{
    "Cut-off Rule":.^42}\n\
\n\n{
    " Current fuel consumption :":>15}{
    oil_wear:.2f}  l /100km\
\n\n{
    "":>19}{
    oil_price * oil_wear / 100:.2f}  element /km\
\n\n\n{
    "The End":_^42}\n')



Home

my HOT Bo :

    原网站

    版权声明
    本文为[Fantasy elves_ cq]所创,转载请带上原文链接,感谢
    https://yzsam.com/2022/02/202202140710217417.html