由于下面的文档字符串状态,我试图编写一个需要3个参数(浮点数)并返回一个值的Python代码。例如,输入低1.0,高9.0和0.25。这返回3.0,这是1.0和9.0之间的数字的25%。这是我想要的,下面的“回归”方程是正确的。我可以在python shell中运行它,它给了我正确的答案。蟒蛇如何运行与三个输入功能
但是,当我运行此代码,试图提示用户输入时,口口声声说:
“NameError:名字为‘低’没有定义”
我只是想运行它,并获得提示:“Enter low,hi,fraction:”然后用户输入例如“1.0,9.0,0.25”,然后返回“3.0”。
如何定义这些变量?我如何构建打印语句?我如何得到这个运行?
def interp(low,hi,fraction): #function with 3 arguments
""" takes in three numbers, low, hi, fraction
and should return the floating-point value that is
fraction of the way between low and hi.
"""
low = float(low) #low variable not defined?
hi = float(hi) #hi variable not defined?
fraction = float(fraction) #fraction variable not defined?
return ((hi-low)*fraction) +low #Equation is correct, but can't get
#it to run after I compile it.
#the below print statement is where the error occurs. It looks a little
#clunky, but this format worked when I only had one variable.
print (interp(low,hi,fraction = raw_input('Enter low,hi,fraction: ')))
'low,hi,fraction = map(float,raw_input('Enter low,hi,fraction:').split(“,”))' –
谢谢,我也可以使用它!非常感激! – Tyler