import sys

# Initialize conversion history list
history = []

def show_menu():
    print("\n=== Ultimate Temperature Converter & History ===")
    print("Select an option:")
    print("  1. View Absolute Zero Info")
    print("  2. Celsius to Kelvin & Fahrenheit")
    print("  3. Kelvin to Celsius & Fahrenheit")
    print("  4. Fahrenheit to Celsius & Kelvin")
    print("  5. Celsius to Fahrenheit")
    print("  6. Fahrenheit to Celsius")
    print("  7. Convert any temperature to all scales")
    print("  8. Show temperature scales")
    print("  9. Show conversion history")
    print("  10. Exit")

def get_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            print("Invalid input. Please enter a numeric value.")

def show_absolute_zero():
    print("\nAbsolute Zero is the lowest possible temperature:")
    print("  Celsius: -273.15°C")
    print("  Fahrenheit: -459.67°F")
    print("  Kelvin: 0 K")
    print("Particles have minimal thermal motion at this temperature.")

def celsius_to_kelvin_fahrenheit():
    c = get_float("Enter temperature in Celsius: ")
    if c < -273.15:
        print("Error: Temperature below absolute zero.")
        return
    k = c + 273.15
    f = (c * 9/5) + 32
    print(f"\nResults:\n  Celsius: {c}°C\n  Kelvin: {k} K\n  Fahrenheit: {f}°F")
    history.append(("C to K/F", f"C: " + str(c), "K: " + str(k) + ", F: " + str(f)))

def kelvin_to_c_f():
    k = get_float("Enter temperature in Kelvin: ")
    if k < 0:
        print("Error: Kelvin cannot be negative.")
        return
    c = k - 273.15
    f = (c * 9/5) + 32
    print(f"\nResults:\n  Kelvin: {k} K\n  Celsius: {c}°C\n  Fahrenheit: {f}°F")
    history.append(("K to C/F", "K: " + str(k), "C: " + str(c) + ", F: " + str(f)))

def fahrenheit_to_c_k():
    f = get_float("Enter temperature in Fahrenheit: ")
    c = (f - 32) * 5/9
    if c < -273.15:
        print("Error: Result below absolute zero.")
        return
    k = c + 273.15
    print(f"\nResults:\n  Fahrenheit: {f}°F\n  Celsius: {c}°C\n  Kelvin: {k} K")
    history.append(("F to C/K", "F: " + str(f), "C: " + str(c) + ", K: " + str(k)))

def celsius_to_fahrenheit():
    c = get_float("Enter temperature in Celsius: ")
    if c < -273.15:
        print("Error: Temperature below absolute zero.")
        return
    f = (c * 9/5) + 32
    print(f"\nResults:\n  Celsius: {c}°C\n  Fahrenheit: {f}°F")
    history.append(("C to F", "C: " + str(c), "F: " + str(f)))

def fahrenheit_to_c():
    f = get_float("Enter temperature in Fahrenheit: ")
    c = (f - 32) * 5/9
    if c < -273.15:
        print("Error: Result below absolute zero.")
        return
    print(f"\nResults:\n  Fahrenheit: {f}°F\n  Celsius: {c}°C")
    history.append(("F to C", "F: " + str(f), "C: " + str(c)))

def convert_any():
    print("\nChoose the scale of your input:")
    print("  1. Celsius")
    print("  2. Fahrenheit")
    print("  3. Kelvin")
    choice = input("Enter choice (1-3): ").strip()
    if choice == '1':
        c = get_float("Enter Celsius: ")
        if c < -273.15:
            print("Error: Below absolute zero.")
            return
        f = (c * 9/5) + 32
        k = c + 273.15
        print(f"\nConversions:\n  Celsius: {c}°C\n  Fahrenheit: {f}°F\n  Kelvin: {k} K")
        history.append(("Any Input", "C: " + str(c), "F: " + str(f) + ", K: " + str(k)))
    elif choice == '2':
        f = get_float("Enter Fahrenheit: ")
        c = (f - 32) * 5/9
        if c < -273.15:
            print("Error: Result below absolute zero.")
            return
        k = c + 273.15
        print(f"\nConversions:\n  Fahrenheit: {f}°F\n  Celsius: {c}°C\n  Kelvin: {k} K")
        history.append(("Any Input", "F: " + str(f), "C: " + str(c) + ", K: " + str(k)))
    elif choice == '3':
        k = get_float("Enter Kelvin: ")
        if k < 0:
            print("Error: Kelvin cannot be negative.")
            return
        c = k - 273.15
        f = (c * 9/5) + 32
        print(f"\nConversions:\n  Kelvin: {k} K\n  Celsius: {c}°C\n  Fahrenheit: {f}°F")
        history.append(("Any Input", "K: " + str(k), "C: " + str(c) + ", F: " + str(f)))
    else:
        print("Invalid choice.")

def show_history():
    if not history:
        print("\nNo conversions yet.")
    else:
        print("\n=== Conversion History ===")
        for i, record in enumerate(history, 1):
            print(f"{i}. {record[0]} -> {record[1]} | {record[2]}")

def show_temperature_scales():
    print("Temperature Scales Overview:")
    print()
    print("1. Celsius (C): based on water's freezing (0 C) and boiling points (100 C).")
    print("2. Fahrenheit (F): water freezes at 32 F, boils at 212 F.")
    print("3. Kelvin (K): absolute scale starting at 0 K (absolute zero).")
    print()
    print("Note: Absolute zero is -273.15 C, -459.67 F, 0 K.")

def main():
    while True:
        show_menu()
        choice = input("Enter your choice (1-9): ").strip()
        if choice == '1':
            show_absolute_zero()
        elif choice == '2':
            celsius_to_kelvin_fahrenheit()
        elif choice == '3':
            kelvin_to_c_f()
        elif choice == '4':
            fahrenheit_to_c_k()
        elif choice == '5':
            celsius_to_fahrenheit()
        elif choice == '6':
            fahrenheit_to_c()
        elif choice == '7':
            convert_any()
        elif choice == '8':
            show_temperature_scales()
        elif choice == '9':
            show_history()
        elif choice == '10':
            print("Goodbye!")
            break
        else:
            print("Invalid choice. Please select 1-9.")

if __name__ == "__main__":
    main()
