Problems using user input between functions
I am trying to learn Python and I am really struggling to keep my code in standalone functions. Here's an example:
def get_inputs():
sales_amount = float(input("Enter total sales amount: "))
def calculate_discount(sales_amount):
discount_amount = sales_amount * 2
return discount_amount
def output():
print ( discount_amount )
def main():
get_inputs()
calculate_discount(sales_amount)
output()
main()
Making this return
File "/Users/Desktop/assA3.py", line 17, in <module>
main()
File "/Users/Desktop/assA3.py", line 14, in main
calculate_discount(sales_amount)
NameError: name 'sales_amount' is not defined
I thought the variable sales_amount
was user-defined before referring to it later. I cannot figure out what I am missing.
I apologize for how basic this problem is, but I'm clearly missing something fundamental and I'm just scared. Any help would be greatly appreciated. Thank.
source to share
You are trying to use function scope variables in other functions. Nothing outside the function get_inputs
can see the variable sales_amount
, its variable is local to that function. You will run into the same problem with discount_amount
which is tied to calculate_discount
.
Instead, return values ββand pass them to other functions.
def get_inputs():
return float(input("Enter total sales amount: "))
def calculate_discount(sales_amount):
discount_amount = sales_amount * 2
return discount_amount
def output(discount_amount):
print ( discount_amount )
def main():
inputs = get_inputs()
discounts = calculate_discount(inputs)
output(discounts)
main()
A dirty option is to make them global
def get_inputs():
global sales_amount
sales_amount = float(input("Enter total sales amount: "))
def calculate_discount(sales_amount):
global discount_amount
discount_amount = sales_amount * 2
return discount_amount
def output():
print ( discount_amount )
def main():
get_inputs()
calculate_discount(sales_amount)
output()
main()
Globals are slower, make your code harder to maintain, and are bad for a variety of reasons.
source to share
The simple answer is to pass sales_amount
from gets_input()
:
def get_inputs():
sales_amount = float(input("Enter total sales amount: "))
return sales_amount
and use it in main()
:
def main():
sales_amount = get_inputs()
calculate_discount(sales_amount)
output()
If you want to make it "self-contained" you want to use classes:
class Sales(object):
def get_inputs(self):
self.sales_amount = float(input("Enter total sales amount: "))
def calculate_discount(self):
self.discount_amount = self.sales_amount * 2
def output(self):
print ( self.discount_amount )
def main():
my_sale = Sales()
my_sale.get_inputs()
my_sale.calculate_discount()
my_sale.output()
main()
source to share
in each of get_inputs, calculate_discounts and main, sales_amount is a local variable. It might make more sense if each routine uses a different variable name, where all three are currently using sales_amount.
I suggest that get_inputs return a result and main stores the result of get_inputs into a variable, which is then passed to calculate_discounts.
source to share
This is because variables defined in functions are local to Python.
See fooobar.com/questions/1950 / ... to help you understand where you can access variables from.
In your case, you should just pass sales_amount as a parameter to your function as others have pointed out :)
source to share