0

I am just learning functions so please go easy on me! I do not have a problem calling the function with nothing as a parameter but when I put a parameter in and try to call it. It does not work?

This is what I am supposed to do:

Code a function called getFloatInput that receives a string as a parameter to be used as the prompt input text and it returns a float. You will be calling this function for each value to be inputted and assign the function’s return value and assign to each of the listed variables. For example: fSalesPrice = getFloatInput(“Enter property sales value:”)

def getFloatInput(sales):
    salesPrice = 0
    while True:
        try:
            salesPrice = float(input("Enter property sales value: "))
            if salesPrice <= 0:
                print("Enter a numeric value greater than 0")
                return salesPrice
        except ValueError:
            print("Input must be a numeric value")
        return salesPrice


getFloatInput(salesPrice)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
meg
  • 73
  • 6
  • 3
    You don't define `sales` before `getFloatInput(sales)` – Corralien Feb 19 '22 at 23:26
  • 5
    Why do you use a parameter "sales" to your function that is never unsed inside of it? – Patrick Artner Feb 19 '22 at 23:26
  • Try `getFloatInput("sales")` or `getFloatInput(50)` – TheMaster Feb 19 '22 at 23:27
  • I want to return the input into the function – meg Feb 19 '22 at 23:27
  • I meant to puts salesPrice when I call the function – meg Feb 19 '22 at 23:28
  • 1
    @meg In that case, you should put the input outside and then put the result into the function. – LuckElixir Feb 19 '22 at 23:29
  • I have to do it that way. I have to put a parameter that recieves a string that uses the input. It is in the assignment – meg Feb 19 '22 at 23:29
  • What do you mean by "return the input into the function"? You have `return salesPrice`, which returns the input *from* the function, is that what you mean? Or do you mean you want to *pass* the input into the function? If so, why does the function still have an `input()` call inside it? I really don't understand the problem. Please [edit] the post to clarify. – wjandrea Feb 19 '22 at 23:33
  • So, it says right there, `fSalesPrice = getFloatInput(“Enter property sales value:”)`. Why are you doing something totally different? – wjandrea Feb 19 '22 at 23:37
  • because I am supposed to put the input into the function – meg Feb 19 '22 at 23:41
  • Also, maybe this is beside the point, but the input validation doesn't work; it'll return bad values instead of asking again. You might want to read [Asking the user for input until they give a valid response](/q/23294658/4518341). – wjandrea Feb 19 '22 at 23:46
  • @meg Right, the function is supposed to take input from the user. But why are you trying to pass a nonexistent variable into it, when the instructions give a real example of how you're supposed to use it? – wjandrea Feb 19 '22 at 23:49
  • I dont know I have such a hard time understanding how functions work and I should know this by now – meg Feb 19 '22 at 23:50
  • what do I pass into the call function in order for it to use the input when I call that function? – meg Feb 19 '22 at 23:51
  • OK, I see. Are you taking a college course? I'd recommend talking to your professor instead of asking here, because we don't know what you've already learned and how you've learned it. SO is not built to teach you the language; we just assume you already know it. (Related: [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341).) If you're self-learning, you might try asking on, say, [LearnPython on Reddit](https://reddit.com/r/learnpython/). – wjandrea Feb 19 '22 at 23:55
  • *"what do I pass into the call function in order for it to use the input when I call that function?"* -- To be totally honest, that question doesn't make any sense. I don't think you understand what "call" and "pass in" mean. – wjandrea Feb 19 '22 at 23:56
  • He never answers unfortunetly – meg Feb 19 '22 at 23:57
  • I think calling the function is what I am doing when I do getFloatInput(salesPrice) at the very end. You have to call the function in order for it to work. What I do not understand is passing values into the function. – meg Feb 19 '22 at 23:58

1 Answers1

1

sales is a local variable inside the scope of your function getFloatInput, thus you are trying to access a local variable outside of it's scope - which is the global scope in your case.

Assuming you have not defined it in the global scope, you are trying to use a variable which is not defined by calling getFloatInput(sales) and get an exception in consequence.

You should read up on those fundamental concepts:

https://www.w3schools.com/python/python_scope.asp https://www.w3schools.com/python/python_variables.asp

Bialomazur
  • 1,122
  • 2
  • 7
  • 18
  • I want to have the function take in that salesPrice input as a string parameter. It is what I have to do in the assignment. It works when I put it outside but I am supposed to have the input inside the function and then call it – meg Feb 19 '22 at 23:33
  • 1
    @meg You should edit your question so it is more clearer for other people – LuckElixir Feb 19 '22 at 23:34
  • 1
    I didnt know I could edit until now lol – meg Feb 19 '22 at 23:35