python 运行 如何使用三个输入
python how run function with three inputs
正如下面的文档字符串所述,我正在尝试编写一个 python 代码,该代码采用 3 个参数(浮点数)和 return 一个值。例如,输入低 1.0、高 9.0 和分数 0.25。这个 returns 3.0 是 1.0 和 9.0 之间的 25% 的数字。这就是我想要的,下面的 "return" 等式是正确的。我可以 运行 它在 python shell 中,它给了我正确的答案。
但是,当我 运行 这段代码试图提示用户输入时,它一直说:
"NameError: name 'low' is not defined"
我只想 运行 它并得到提示:"Enter low, hi, fraction: " 然后用户输入,例如,“1.0, 9.0, 0.25” 然后它会 return "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: ')))
raw_input()
returns 只是 一个 字符串。您要么需要使用 raw_input()
三次,要么需要接受逗号分隔值并将它们分开。
问 3 个问题就容易多了:
low = raw_input('Enter low: ')
high = raw_input('Enter high: ')
fraction = raw_input('Enter fraction: ')
print interp(low, high, fraction)
但拆分也可以:
inputs = raw_input('Enter low,hi,fraction: ')
low, high, fraction = inputs.split(',')
如果用户没有给出 3 个中间带有逗号的值,这将失败。
您自己的尝试被 Python 视为传递了两个位置参数(传递了变量 low
和 hi
的值),以及一个带有值的关键字参数取自 raw_input()
调用(一个名为 fraction
的参数)。由于没有变量 low
和 hi
你会在 raw_input()
调用甚至被执行之前得到一个 NameError
。
正如下面的文档字符串所述,我正在尝试编写一个 python 代码,该代码采用 3 个参数(浮点数)和 return 一个值。例如,输入低 1.0、高 9.0 和分数 0.25。这个 returns 3.0 是 1.0 和 9.0 之间的 25% 的数字。这就是我想要的,下面的 "return" 等式是正确的。我可以 运行 它在 python shell 中,它给了我正确的答案。
但是,当我 运行 这段代码试图提示用户输入时,它一直说:
"NameError: name 'low' is not defined"
我只想 运行 它并得到提示:"Enter low, hi, fraction: " 然后用户输入,例如,“1.0, 9.0, 0.25” 然后它会 return "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: ')))
raw_input()
returns 只是 一个 字符串。您要么需要使用 raw_input()
三次,要么需要接受逗号分隔值并将它们分开。
问 3 个问题就容易多了:
low = raw_input('Enter low: ')
high = raw_input('Enter high: ')
fraction = raw_input('Enter fraction: ')
print interp(low, high, fraction)
但拆分也可以:
inputs = raw_input('Enter low,hi,fraction: ')
low, high, fraction = inputs.split(',')
如果用户没有给出 3 个中间带有逗号的值,这将失败。
您自己的尝试被 Python 视为传递了两个位置参数(传递了变量 low
和 hi
的值),以及一个带有值的关键字参数取自 raw_input()
调用(一个名为 fraction
的参数)。由于没有变量 low
和 hi
你会在 raw_input()
调用甚至被执行之前得到一个 NameError
。