I'm trying to learn Python and I'm really struggling with getting my code into self-contained functions. Here is 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()
Running this returns
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 defined by the user input before it is referenced later. I can't figure out what I'm missing.
I apologise for how basic this problem is but I'm clearly misunderstanding something fundamental and I'm just really struggling. Any help would be greatly appreciated. Thanks.
You are trying to use function scope variables in other functions. Nothing outside of function get_inputs can see variable sales_amount, its a variable local to that function. You will encounter the same issue with discount_amount, which is scoped to calculate_discount.
Instead return the 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()
The dirty option is to make them globals
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 more difficult to maintain and are bad for a whole raft of reasons.
The simple answer is to pass sales_amount out of gets_input():
def get_inputs():
sales_amount = float(input("Enter total sales amount: "))
return sales_amount
and to use it in main():
def main():
sales_amount = get_inputs()
calculate_discount(sales_amount)
output()
If you want to make it "self-contained" then 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With