Skip to content

Commit bb970fa

Browse files
authored
Add ounces to grams conversion.
Adds a simple cooking-related unit conversion using the avoirdupois ounce.
1 parent 3c88735 commit bb970fa

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

conversions/ounces_to_grams.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Convert ounces to grams.
3+
4+
This conversion uses the avoirdupois ounce:
5+
1 ounce = 28.3495 grams
6+
"""
7+
8+
def ounces_to_grams(ounces: float) -> float:
9+
"""
10+
Convert ounces to grams.
11+
12+
:param ounces: Weight in ounces
13+
:return: Weight in grams
14+
"""
15+
if ounces < 0:
16+
raise ValueError("Weight cannot be negative")
17+
18+
return ounces * 28.3495
19+
20+
21+
if __name__ == "__main__":
22+
ounces = float(input("Enter weight in ounces: "))
23+
grams = ounces_to_grams(ounces)
24+
print(f"{ounces} ounces is equal to {grams:.2f} grams")

0 commit comments

Comments
 (0)