TORONTO KIDS COMPUTER CLUB | Monday 20:00 Python Practice 20.09.28.
18371
post-template-default,single,single-post,postid-18371,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.8.0,vc_responsive

Monday 20:00 Python Practice 20.09.28.

01 Oct Monday 20:00 Python Practice 20.09.28.

Question:

Create an FluidConversion object to convert any number of fluid units to the equivalent number of gallons, quarts, pints, and gills. (There are 128 fluid ounces in a gallon. There are 32 fluid ounces in a quart. There are 16 fluid ounces in a pint. There are 4 fluid ounces in a gill.)

  • Get the number of fluid gallons, quarts, pints, gills and ounces as integer from the user; make sure to use a descriptive prompt so the user knows what to enter, for example:
Sample input:
Please enter fluid gallons:
Please enter fluid quarts:
Please enter fluid pints:
Please enter fluid gills:
Please enter fluid ounces:
  • The object contains methods to calculate the total number of gallons, quarts, pints, and gills represented by the original number of fluid ounces and print it out. for example:
    • convertGallons()    – it just simply convert into gallons, the method will have a return value
    • convertQuarts()    – it just simply convert into quarts, the method will have a return value
    • convertPints()    – it just simply convert into pints, the method will have a return value
    • convertGills()    – it just simply convert into gills, the method will have a return value
    • convertOunces()    – it just simply convert into the ounces, the method will have a return value
    • convert()   –  convert any number of fluid units to the equivalent number of gallons, quarts, pints, and gills. for example:
      • “1 Gallon 6523 fluid ounces is 51 gallon(s)+3 quart(s)+1 pint(s)+2 gill(s)+3 ounce(s)”
  • Define __str__(self)  show calculated number of gallons, quarts, pints, gills and ounce in one line, for example:
    • “6523 fluid ounces is 50 gallon(s)+3 quart(s)+1 pint(s)+2 gill(s)+3 ounce(s)”

Sample program as below:

class FluidConversion:
def __init__(self, gallons=0, quarts=0, pints=0, gills=0, ounces=0):
...

def convertGallons(self):
...

def convertQuarts(self):
...

def convertPints(self):
...

def convertGills(self):
...

def convertOunces(self):
...

def convert(self):
...

def __str__(self):
...

gallons = int(input('Gallons: '))
quarts = int(input('Quarts: '))
pints = int(input('Pints: '))
gills = int(input('Gills: '))
ounces = int(input('Ounces: '))
fc = FluidConversion(gallons, quarts, pints, gills, ounces)
print('it is', fc.convertGallons(), 'gallons')
print('it is', fc.convertQuarts(), 'quarts')
print('it is', fc.convertPints(), 'pints')
print('it is', fc.convertGills(), 'gills')
print('it is', fc.convertOunces(), 'ounces')
print(fc)
fc.convert()
print(fc)

1Comment
  • Jesse Shi
    Posted at 21:41h, 03 October Reply

    class FluidConversion:
    def __init__(self, gallons=0, quarts=0, pints=0, gills=0, ounces=0):

    def convertGallons(self):
    = (gallons)
    = 4 quarts
    = 8 pints
    = 32 gills
    = 128 ounces

    def convertQuarts(self):
    = (quaters)
    = 2 pints
    = 8 gills
    = 32 ounces

    def convertPints(self):
    = (pints)
    = 4 gills
    = 16 ounces

    def convertGills(self):
    = (gills)
    = 4 ounces

    def convertOunces(self):

    def convert(self):

    def __str__(self):

    gallons = int(input(‘Gallons: ‘))

    quarts = int(input(‘Quarts: ‘))

    pints = int(input(‘Pints: ‘))

    gills = int(input(‘Gills: ‘))

    ounces = int(input(‘Ounces: ‘))

    fc = FluidConversion(gallons, quarts, pints, gills, ounces)
    print(‘it is’, fc.convertGallons(), ‘gallons’)
    print(‘it is’, fc.convertQuarts(), ‘quarts’)
    print(‘it is’, fc.convertPints(), ‘pints’)
    print(‘it is’, fc.convertGills(), ‘gills’)
    print(‘it is’, fc.convertOunces(), ‘ounces’)
    print(fc)
    fc.convert()
    print(fc)

    I don’t reallyu get the convert, bit I think for the ounces is to divide by 128, and then by 32, by 16, 4.
    And same for tghe other ones except for gallon.
    I don’t know how to make it divide it and then divide the remainder.

Post A Comment