Conversor de Temperatura em Python

Publicado por João Vitor (última atualização em 03/02/2021)

[ Hits: 5.264 ]

Download 7107.temperature-conversion.py




Está em inglês mas é simples de entender ( e está em inglês porque foi para eu subir no meu GitHub - https://github.com/VitorSorriso ).

* converte °C em °F
* converte °F em °C
* converte °C em K
* converte K em °C
* converte °F para K
* converte K em °F

  



Esconder código-fonte

def line():
    print('-'*50)


def celsius():
    result = (temperature*1.8) + 32
    print(f'The temperature {temperature:.1f}°C converted is from {result:.1f}°F')


def fahrenheit():
    result = (temperature - 32) / 1.8
    print(f'The temperature {temperature:.1f}°F converted is from {result:.1f}°C')


def kelvin_c():
    result = (temperature - 273.15)
    print(f'The temperature {temperature:.1f}K converted is from {result:.1f}°C')


def c_kelvin():
    result = (temperature + 273.15)
    print(f'The temperature {temperature:.1f}°C converted is from {result:.1f}k')


def fahrenheit_k():
    result = (temperature - 32) * 5/9 + 273.15
    print(f'The temperature {temperature:.1f}°F converted is from {result:.1f}k')


def k_fahrenheit():
    result = (temperature - 273.15) * 9/5 + 32
    print(f'The temperature {temperature:.1f}K converted is from {result:.1f}°F')


def print_tabela():
    print(
    '''
    [1] Celsius(°C) to Fahrenheit(°F)
    [2] Fahrenheit(°F) to Celsius(°C)
    [3] Kevin(K) to Celsius(°C)
    [4] Celsius(°C) to Kevin(K)
    [5] Fahrenheit(°F) to Kevin(K)
    [6] Kevin(K) to Fahrenheit(°F)
    [7] to close the program
    '''
    )
    
while True:
    line()
    print('Which operation do you want to do?')
    line()
    print_tabela()
    line()

    option = int(input('Enter the option: '))
    if option == 1 or option == 2 or option == 3 or option == 4 or option == 5 or option == 6:
        line()
        temperature = float(input('Enter the temperature you want to convert: '))

        if option == 1:
            celsius()
        elif option == 2:
            fahrenheit()
        elif option == 3:
            kelvin_c()
        elif option == 4:
            c_kelvin()
        elif option == 5:
            fahrenheit_k()
        elif option == 6:
            k_fahrenheit()
        else: 
            print('Invalid command. ERROR')
        
    elif option == 7:
        print('Finishing..')
        break
    else:
        print('Invalid command. ERROR')

Scripts recomendados

Um novo hello World, com checkbox

Blackjack simples para estudo de iniciantes em Python

Números pares até 100

Script para calcular indice de massa corporal

Contagem regressiva ao sair


  

Comentários
[1] Comentário enviado por maurixnovatrento em 03/02/2021 - 18:02h


Boa cara, legal.

___________________________________________________________
[code]Conhecimento não se Leva para o Túmulo.
https://github.com/MauricioFerrari-NovaTrento [/code]

[2] Comentário enviado por removido em 18/02/2021 - 11:38h

Infelimente, não consegui testar o seu código aqui, aparece um erro..
Poderia implementar uma conversão de temperaturas relativas..
Por exemplo, se houver uma variação de 10° Farenheit, qual a variação de temperatura em graus Celsius?

Eu escrevi um conversor de temperatura em Bash (ctemp.sh).
#Modo padrão converte Farenheit para Celsius:
$ ctemp.sh 86
Se não especificar a unidade, ele vai fazer um toggle entre F-->C na primeira vez que rodar,
e C-->F na segunda vez que rodar..

#Celsius para Farenheit:
$ ctemp.sh 30c f

#Farenheit para Kelvin:
$ ctemp.sh 86f k

E por assim vai. Vale a pena ver o pacote `units', porém é mais complexo de usar.
https://github.com/mountaineerbr/scripts/blob/master/ctemp.sh


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts