Python how to run a function with three inputs

Since the doc line is below the states, I am trying to write python code that takes 3 arguments (floating point numbers) and returns one value. For example, entry low is 1.0, hi is 9.0, and fraction is 0.25. This returns 3.0, which is 25% from 1.0 to 9.0. This is what I want, the correct "return" equation is correct. I can run it in python shell and it gives the correct answer.

But, when I run this code to try and request user inputs, it keeps saying:

"NameError: name" low "is undefined"

I just want to run it and get the prompt "Enter low, hi, fraction:" and then the user will enter, for example, "1.0, 9.0, 0.25" and then it will return "3.0".

How do you define these variables? How do I create a print instruction? How to do it?

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: '))) 

      

+3


source to share


1 answer


raw_input()

only returns one row. You either need to use raw_input()

three times or you need to accept comma separated values ​​and separate them.

Asking 3 questions is much easier:

low = raw_input('Enter low: ')
high = raw_input('Enter high: ')
fraction = raw_input('Enter fraction: ')

print interp(low, high, fraction) 

      

but splitting might work as well:



inputs = raw_input('Enter low,hi,fraction: ')
low, high, fraction = inputs.split(',')

      

It will fail unless the user gives exactly 3 values ​​with commas in between.

Your own attempt was seen by Python as passing in two positional arguments (passing values ​​from low

and variables hi

) and a keyword argument with the value taken from the call raw_input()

(name argument fraction

). Since there are no variables low

and hi

, you will get NameError

before the call is made raw_input()

.

+6


source







All Articles