我正在制作一个单位转换器只是为了练习。目前,我已经定义了一个函数来确定要进行哪种类型的转换(距离、时间、质量等)。
然后它调用该类型的正确转换器,询问您要转换的内容、要转换的内容以及转换的值是什么。
def mass_converter():
convert_from = raw_input('What unit are you converting from? ')
convert_to = raw_input('What unit are you converting to? ')
value = raw_input('How many of those do you have? ')
if convert_from == "pounds" and convert_to == "kilograms":
answer = float(value) * 0.453592
print "That many pounds is %d kilograms." % answer
elif convert_from == "kilograms" and convert_to == "pounds":
answer = float(value) * 2.20462
print "That many kilograms is %d pounds." % answer
else:
print "You have not selected a valid unit; please try again."
mass_converter()
目前,如果我将 10 磅换算成公斤,它会告诉我答案是 4 公斤。它似乎正在砍掉我的小数点。显然 int(value) 会做同样的事情。我可以使用什么来保持用户输入的准确值?