'When I call the function it says, name 'sales' is not defined. Can anyone explain why this is happening? [closed]
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
getFloatInputthat 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)
Solution 1:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
