yans
4/28/2020 - 8:49 AM

Konwersja jednostek miar

'''
 Skrypt dokonujący konwersji jednostek miar takich jak:
  * stopnie Celsjusza na Kelvina
  * metry na cale
  * cale na metry
'''

# Dokonuje konwerscji stopni Celsjusza na stopnie Kelvina
def celsius_to_kelvin(celsius):
    return celsius + 273.15 

# Dokonuje konwerscji metrów na cale 
def meters_to_inches(meters):
    return meters * 39.3700787

# Dokonuje konwersji cali na metry
def inches_to_meters(inches):
    return inches * 0.0254

# Wypisuje wyniki na ekranie
def display_output(data, method):
    try:
        unit = float(input(data['message']))
        print(unit, data['unit_in'], '=', method(unit), data['unit_out'])
    except ValueError:
        print('Podana wartość nie jest liczbą, spróbuj ponownie.')
    input("Naciśnij Enter aby kontynuować...")


while True:
    choice = '''1. Konwersja stopni Celsjusza na stopnie Kelvina
2. Konwersja metrów na cale 
3. Konwersja cali na metry
Q. Koniec
Dokonaj wyboru: '''
    
    question = input(choice)

    if (question not in ['1','2','3','q']):
        continue
    else:
        if (question == '1'):
            display_output({
                'message': 'Podaj temperaturę w stopniach Celsjusza: ',
                'unit_in': 'C',
                'unit_out': 'K'
                }, celsius_to_kelvin)
        elif (question == '2'):
            display_output({
                'message': 'Podaj ilość metrów: ',
                'unit_in': 'm',
                'unit_out': 'in'
                }, meters_to_inches)
        elif (question == '3'):
            display_output({
                'message': 'Podaj ilość cali: ',
                'unit_in': 'in',
                'unit_out': 'm'
                }, inches_to_meters)
        elif (question.lower() == 'q'):
            break