我正在编写一个程序,将厘米的给定值转换为英尺和英尺。我写了两个函数;一个函数将以厘米为单位的值转换为英尺,并将该值的小数部分传递给另一个将十进制数转换为英寸的函数。代码如下所示:
import math
def feetConverter(number):
tempInchValue, feetValue = math.modf(int(number) * 0.03281)
inchConverter(tempInchValue) #calls the inch converter function
return feetValue
def inchConverter(tempInchValue):
finalInchValue = tempInchValue * 12
return finalInchValue
print("Enter the height in centimeters: ")
centimeterValue = input()
print(feetConverter(centimeterValue))
问题是程序没有给出英寸值 - 好像没有调用inchConverter函数。我将不胜感激。谢谢
答案 0 :(得分:1)
一个函数只能返回一个值,你打印的返回值是feetConverter
,忽略inchConverter
的返回值
def feetConverter(number):
tempInchValue, feetValue = math.modf(int(number) * 0.03281)
inchConverter(tempInchValue) #calls the inch converter function, and ignores the return value
return feetValue # returns feetValue only.
你应该打电话给&分别打印inchConverter
:
print(inchConverter(centimeterValue))
或者,您可以返回tuple
个值:
def feetConverter(number):
tempInchValue, feetValue = math.modf(int(number) * 0.03281)
return (feetValue,inchConverter(tempInchValue))
答案 1 :(得分:1)
那是家庭作业吗?否则,为什么不使用单位转换套餐?例如数量感觉很好:
>>> import quantities as pq
>>> distance = 3*pq.meter
>>> distance.units = 'feet'
>>> distance
array(9.842519685039369) * ft
>>> distance.units = 'm'
>>> distance
array(2.9999999999999996) * m
>>> distance.units = 'inches'
>>> distance
array(118.1102362204724) * in