Download example_usage.py from NuBest/height-conversion-tool: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/NuBest/height-conversion-tool/resolve/main/example_usage.py
- Command line
-
hf download hf://NuBest/height-conversion-tool/example_usage.py
-
curl -L -o example_usage.py https://huggingface.co/NuBest/height-conversion-tool/resolve/main/example_usage.py
2.85 kB
| """ | |
| Example usage of Height Conversion Tool | |
| """ | |
| from height_converter import HeightConverter | |
| def main(): | |
| """Demonstrate various uses of the HeightConverter.""" | |
| # Initialize converter | |
| converter = HeightConverter() | |
| print("=" * 60) | |
| print("Height Conversion Tool - Examples") | |
| print("=" * 60) | |
| # Example 1: Convert feet/inches to cm | |
| print("\n1. Convert 5'10\" to centimeters:") | |
| height_cm = converter.feet_to_cm(5, 10) | |
| print(f" Result: {height_cm} cm") | |
| # Example 2: Convert cm to feet/inches | |
| print("\n2. Convert 180 cm to feet/inches:") | |
| feet, inches = converter.cm_to_feet(180) | |
| print(f" Result: {feet}'{inches}\"") | |
| # Example 3: Convert meters to feet/inches | |
| print("\n3. Convert 1.75 meters to feet/inches:") | |
| feet, inches = converter.meters_to_feet(1.75) | |
| print(f" Result: {feet}'{inches}\"") | |
| # Example 4: Convert feet to meters | |
| print("\n4. Convert 6 feet to meters:") | |
| meters = converter.feet_to_meters(6, 0) | |
| print(f" Result: {meters} m") | |
| # Example 5: Using string input | |
| print("\n5. Convert using string input:") | |
| heights = ["5'10\"", "180cm", "1.75m"] | |
| for height in heights: | |
| result_cm = converter.convert(height, "cm") | |
| result_feet = converter.convert(height, "feet") | |
| print(f" {height:10} = {result_cm:12} = {result_feet}") | |
| # Example 6: Batch conversion | |
| print("\n6. Batch conversion (cm to feet/inches):") | |
| heights_cm = [150, 160, 170, 180, 190] | |
| print(f" {'CM':<6} {'Feet/Inches':<12}") | |
| print(f" {'-'*6} {'-'*12}") | |
| for height in heights_cm: | |
| feet, inches = converter.cm_to_feet(height) | |
| print(f" {height:<6} {feet}'{inches:.1f}\"") | |
| # Example 7: Height comparison | |
| print("\n7. Height comparison:") | |
| person1 = {"name": "Alice", "height": "5'6\""} | |
| person2 = {"name": "Bob", "height": "175cm"} | |
| alice_cm = converter.feet_to_cm(5, 6) | |
| bob_cm = 175 | |
| print(f" {person1['name']}: {person1['height']} = {alice_cm} cm") | |
| print(f" {person2['name']}: {person2['height']} = {bob_cm} cm") | |
| if alice_cm > bob_cm: | |
| print(f" {person1['name']} is taller by {alice_cm - bob_cm:.1f} cm") | |
| elif bob_cm > alice_cm: | |
| print(f" {person2['name']} is taller by {bob_cm - alice_cm:.1f} cm") | |
| else: | |
| print(f" They are the same height!") | |
| # Example 8: Error handling | |
| print("\n8. Error handling:") | |
| try: | |
| converter.feet_to_cm(-5, 10) | |
| except ValueError as e: | |
| print(f" Caught error: {e}") | |
| try: | |
| converter.parse_height_string("invalid input") | |
| except ValueError as e: | |
| print(f" Caught error: {e}") | |
| print("\n" + "=" * 60) | |
| print("Examples completed!") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| main() | |