File size: 2,853 Bytes
405b09e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
"""
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()
|