3

As the doc string below states, I'm trying to write a python code that takes 3 arguments (float numbers) and returns one value. For example, input low of 1.0, hi of 9.0 and a fraction of 0.25. This returns 3.0 which is the number 25% of the way between 1.0 and 9.0. This is what I want, the "return" equation below is correct. I can run it in an python shell and it gives me the correct answer.

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

"NameError: name 'low' is not defined"

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

How do I define these variables? How do I construct the print statement? How do I get this to run?

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: '))) 
2
  • low,hi,fraction = map(float,raw_input('Enter low,hi,fraction: ').split(",")) Commented Jul 10, 2015 at 22:34
  • Thank you, I can use this as well! Much appreciated! Commented Jul 11, 2015 at 20:29

1 Answer 1

6

raw_input() returns just one string. You'll either need to use raw_input() three times, or you need to accept comma-separated values and split those out.

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 can work too:

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

This'll fail if the user doesn't give exactly 3 values with commas in between.

Your own attempt was seen by Python as passing in two positional arguments (passing in the values from the variables low and hi), and a keyword argument with the value taken from a raw_input() call (an argument named fraction). Since there are no variables low and hi you'd get a NameError before the raw_input() call even is executed.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain a little bit why his code doesn't work, apart from only giving one input? See my comment on the question
Hey, thank you very much for your responses to this. I got it working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.