Created
March 19, 2019 16:28
-
-
Save WorryingWonton/8eeec56b7cee4e7b0f8b14fe1ff31214 to your computer and use it in GitHub Desktop.
Temperature Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def temp_converter(temps, mode, reversed): | |
| return {'f': lambda x, y: [((temp - 32) * 5/9 + 273.15 if not y else (temp - 273.15) * 9/5 + 32) for temp in x], | |
| 'k': lambda x, y: x, | |
| 'c': lambda x, y: [temp + (273.15 if not y else -273.15) for temp in x]}[mode](temps, reversed) | |
| def format_outputs(temps, mode): | |
| return f'°{mode[-1].upper()}, '.join(map(lambda temp: str(round(temp, 2)), temps)) + f'°{mode[-1].upper()}' | |
| if __name__ == '__main__': | |
| mode = input('What temperature scales would you like to convert between? ').lower() | |
| temperatures = list(map(lambda x: float(x), input('Enter temperatures separated by a space: ').split(' '))) | |
| target_temps = temp_converter(temps=temp_converter(temps=temperatures, mode=mode[0], reversed=False), mode=mode[1], reversed=True) | |
| print(format_outputs(target_temps, mode[-1])) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rough Alternate Version which removes the need for the 'reversed' parameter in temp_converter: