You can use round()
one that will make it easier for you to specify decimals:
>>> round(1.23234, 2)
1.23
So, to apply this to your code, you first need to save them as string
so that you can determine how many rounds to round to, and then round at the end:
print ("Welcome to the Imperial to Metric converter")
choice = int(input("For Fahrenheit to Celsius press 1, Feet to Meters press 2, Pounds to Kilograms press 3: "))
if choice not in (1, 2, 3):
print ("Choose a valid function")
else:
s = input("Select the value you wish to convert: ")
value = float(s)
def convert(value):
if choice == 1:
return (value - 32) * (5/9)
elif choice == 2:
return value / 3.2808
elif choice == 3:
return value / 2.2046
print(round(float(convert(value)), len(s.split(".")[1])))
which when entering:
count <== 1
value <== 567.123
gave the correct result:
297.291
source
share