88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
# weight temperature height
|
|
import questionary
|
|
|
|
|
|
def is_float(s):
|
|
"""
|
|
Checks if a given string can be successfully converted to a float.
|
|
|
|
Args:
|
|
s: The string to check.
|
|
|
|
Returns:
|
|
True if the string is a valid float, False otherwise.
|
|
"""
|
|
try:
|
|
float(s)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def weight():
|
|
way = questionary.select("Which way?", ["kg to lb", "lb to kg", "Back"]).ask()
|
|
if way == "kg to lb":
|
|
weight = questionary.text(
|
|
"Enter the kg weight:",
|
|
validate=lambda text: True if is_float(text) else False,
|
|
).ask()
|
|
print("Your converted weight is:", round(float(weight) * 2.2, 2))
|
|
elif way == "lb to kg":
|
|
weight = questionary.text(
|
|
"Enter the lb weight:",
|
|
validate=lambda text: True if is_float(text) else False,
|
|
).ask()
|
|
print("Your converted weight is:", round(float(weight) / 2.2, 2))
|
|
|
|
|
|
def temperature():
|
|
way = questionary.select("Which way?", ["C to F", "F to C", "Back"]).ask()
|
|
if way == "C to F":
|
|
temp = questionary.text(
|
|
"Enter the C temperature:",
|
|
validate=lambda text: True if is_float(text) else False,
|
|
).ask()
|
|
print("Your converted temperature is:", round((float(temp) * 1.8) + 32, 2))
|
|
elif way == "F to C":
|
|
temp = questionary.text(
|
|
"Enter the F temperature:",
|
|
validate=lambda text: True if is_float(text) else False,
|
|
).ask()
|
|
print("Your converted temperature is:", round((float(temp) - 32) * 5 / 9, 2))
|
|
|
|
|
|
def height():
|
|
way = questionary.select("Which way?", ["in to cm", "cm to in", "Back"]).ask()
|
|
if way == "in to cm":
|
|
height = questionary.text(
|
|
"Enter the in height:",
|
|
validate=lambda text: True if is_float(text) else False,
|
|
).ask()
|
|
print("Your converted height is:", round(float(height) * 2.54, 2))
|
|
elif way == "cm to in":
|
|
height = questionary.text(
|
|
"Enter the cm height:",
|
|
validate=lambda text: True if is_float(text) else False,
|
|
).ask()
|
|
print("Your converted height is:", round(float(height) / 2.54, 2))
|
|
|
|
|
|
def main():
|
|
while True:
|
|
choice = questionary.select(
|
|
"Which type of unit do you want to convert?",
|
|
["Weight", "Temperature", "Height", "Exit"],
|
|
).ask()
|
|
if choice == "Exit":
|
|
break
|
|
if choice == "Weight":
|
|
weight()
|
|
elif choice == "Temperature":
|
|
temperature()
|
|
elif choice == "Height":
|
|
height()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|