Ad
Code
Diff
  • """ONLY HERE TO PASS THE TESTS"""
    """THE REST OF THE PROGRAM RUNS WITHOUT THESE"""
    
    #
    #
    #
    
    def celsius_to_fahrenheit(fahrenheit):
        """Converts fahrenheit to celsius."""
        return (fahrenheit - 32) * 5 / 9
    
    
    def fahrenheit_to_celsius(celsius):
        """Converts celsius to fahrenheit."""
        return (celsius * 9 / 5) + 32
    
    """ONLY HERE TO PASS THE TESTS"""
    """THE REST OF THE PROGRAM RUNS WITHOUT THESE"""
    
    #
    #
    #
    
    error_message = lambda text: f"\033[31m{text}\033[0m"
    highlight_mode = lambda text: f"\033[36m{text}\033[0m"
    
    if __name__ == "__main__":
        while True:
            choice = input("Would you like to convert (c)elsius, (f)ahrenheit or (q)uit:\n>")
            try:
                {
                    "c": lambda x: print(highlight_mode(f"{((x * 9 / 5) + 32):.1f}° Fahrenheit")),
                    "f": lambda x: print(highlight_mode(f"{((x - 32) * 5 / 9):.1f}° Celsius"))
                }[choice](int(input(f"""{highlight_mode(f'Converting {"Celsius to Fahrenheit" if choice == "c" else "Fahrenheit to Celsius"}')}\nEnter temperature in {'Celcius' if choice == 'c' else 'Fahrenheit'}:\n>""")))
            except:
                if choice == "q": break
                print(error_message("Invalid input"))
    
        print("Goodbye!")
    • import sys
    • """ONLY HERE TO PASS THE TESTS"""
    • """THE REST OF THE PROGRAM RUNS WITHOUT THESE"""
    • #
    • #
    • #
    • def celsius_to_fahrenheit(fahrenheit):
    • """Converts fahrenheit to celsius."""
    • return (fahrenheit - 32) * 5 / 9
    • def fahrenheit_to_celsius(celsius):
    • """Converts celsius to fahrenheit."""
    • return (celsius * 9 / 5) + 32
    • """ONLY HERE TO PASS THE TESTS"""
    • """THE REST OF THE PROGRAM RUNS WITHOUT THESE"""
    • def get_temperature(metrics: str) -> int:
    • """Gets and returns the temperature.
    • #
    • #
    • #
    • :param metrics: will be either 'celsius' or 'fahrenheit'
    • :return: the temperature the user inputs.
    • """
    • if metrics == 'celsius':
    • print(highlight_mode(f'Converting Celsius to Fahrenheit'))
    • else:
    • print(highlight_mode(f'Converting Fahrenheit to Celsius'))
    • error_message = lambda text: f"\033[31m{text}\033[0m"
    • highlight_mode = lambda text: f"\033[36m{text}\033[0m"
    • if __name__ == "__main__":
    • while True:
    • # validate user input
    • user_input = input(f'Enter temperature in {metrics}:
    • > ')
    • # if user_input is not valid:
    • if not user_input.isdigit():
    • print(error_message('Invalid input'))
    • # continue asking for input
    • continue
    • else:
    • break
    • return int(user_input)
    • choice = input("Would you like to convert (c)elsius, (f)ahrenheit or (q)uit:
    • >")
    • try:
    • {
    • "c": lambda x: print(highlight_mode(f"{((x * 9 / 5) + 32):.1f}° Fahrenheit")),
    • "f": lambda x: print(highlight_mode(f"{((x - 32) * 5 / 9):.1f}° Celsius"))
    • }[choice](int(input(f"""{highlight_mode(f'Converting {"Celsius to Fahrenheit" if choice == "c" else "Fahrenheit to Celsius"}')}\nEnter temperature in {'Celcius' if choice == 'c' else 'Fahrenheit'}:\n>""")))
    • except:
    • if choice == "q": break
    • print(error_message("Invalid input"))
    • def get_metrics() -> str:
    • """Returns User's selects of conversion; either celsius or fahrenheit."""
    • while True:
    • user_input = input('Would you like to convert (c)elsius, (f)ahrenheit, or (q)uit:\n> ')
    • # if invalid, continue asking for input
    • if user_input not in 'cfq':
    • print(error_message('Invalid input'))
    • # continue asking for input
    • continue
    • else:
    • break
    • # If user selects 'q' for quit:
    • if user_input == 'q':
    • print('Goodbye!')
    • sys.exit()
    • # If user selects 'c' for celsius:
    • elif user_input == 'c':
    • return 'celsius'
    • # If user selects 'f' for fahrenheit
    • return 'fahrenheit'
    • def convert_temperature(temperature: int, metrics: str) -> None:
    • """Prints temperature in either celsius or fahrenheit.
    • :param temperature: the temperature that will be converted
    • :param metrics: will be either 'celsius' or 'fahrenheit'
    • :return: the temperature converted in either 'celsius' or 'fahrenheit'
    • """
    • if metrics == 'celsius':
    • print(highlight_mode(f'{celsius_to_fahrenheit(temperature)}° celsius'))
    • else:
    • print(highlight_mode(f'{fahrenheit_to_celsius(temperature)}° fahrenheit'))
    • def highlight_mode(text: str) -> str:
    • """teal color font to highlight output."""
    • return f'\033[36m{text}\033[0m'
    • def error_message(text: str) -> str:
    • """red color font to highlight error messages."""
    • return f'\033[31m{text}\033[0m'
    • def main():
    • """Main program."""
    • while True:
    • # get the metrics
    • metrics = get_metrics()
    • # get the temperature
    • temperature = get_temperature(metrics)
    • # convert temperature
    • convert_temperature(temperature, metrics)
    • if __name__ == '__main__':
    • main()
    • print("Goodbye!")