Reputation: 45
I keep on getting an error with my discount variable. name-error: global name 'discount' not defined. please take a look at my code and help me out. I don't want to modify the parameters of the functions at all.
def finddiscount(quantity):
if quantity >= 1 and quantity <= 9:
discount = 0
elif quantity >= 10 and quantity <= 19:
discount = .2
elif quantity >= 20 and quantity <= 49:
discount = .30
elif quantity >= 50 and quantity <= 99:
discount = .40
elif quantity >= 100:
discount = .50
return discount
def calctotal(quantity, price):
finddiscount(quantity)
disc = (price*quantity)*discount
total = (price*quantity)
due = (price*quantity)-(price*quantity)*dicount
print ("\t","Order total $",format(total, "10.2"),"\n\t","Discount $",format(disc,"10.2"),"\n\t","Amount Due $",format (due, "10.2"),sep="")
def main():
quantity = int(input("How many packages where purchased?"))
price = float(input("How much is each item?"))
calctotal(quantity, price)
main()
Upvotes: 1
Views: 1213
Reputation: 2496
If we look at this block of code
def calctotal(quantity, price):
finddiscount(quantity)
disc = (price*quantity)*discount
total = (price*quantity)
due = (price*quantity)-(price*quantity)*dicount
print ("\t","Order total $",format(total, "10.2"),"\n\t","Discount $",format(disc,"10.2"),"\n\t","Amount Due $",format (due, "10.2"),sep="")
neither discount
nor dicount
(I'm assuming typo) have been declared in calctotal()
. Below should solve yoru issue
def calctotal(quantity, price):
discount = finddiscount(quantity)
disc = (price*quantity)*discount
total = (price*quantity)
due = (price*quantity)-(price*quantity)*discount
print ("\t","Order total $",format(total, "10.2"),"\n\t","Discount $",format(disc,"10.2"),"\n\t","Amount Due $",format (due, "10.2"),sep="")
EDIT:
While making discount
a global variable is certainly a way to do this, in general I'd recommend not making a variable global unless you have a good reason. For example - if the discount applied was going to be affected by external functions, and you wanted it changed in finddiscount()
as well then a global variable would work nicely. However in this scenario you are creating all possible values of discount
within finddiscount()
and it just makes more sense to use assignment.
The problem with global variables is that you can accidentally reassign them to things you didn't intend to do, and it can clutter up the namespace if done for no reason.
Upvotes: 0
Reputation: 8009
You have to declare discount
as a global if you want to access it in a multi block scope.
discount = 0
def finddiscount(quantity):
...
global discount # Needed to modify global copy of discount
discount = 1
Upvotes: 3
Reputation:
disc = (price*quantity)*discount
You never defined discount
in calctotal
(and neither in global scope). Assign the result from finddiscount(quantity)
to it. At the moment you are calculating the discount, but drop the result immediately by not assigning it to any variable:
def calctotal(quantity, price):
discount = finddiscount(quantity)
disc = (price*quantity)*discount
total = (price*quantity)
due = (price*quantity)-(price*quantity)*dicount
print ("\t","Order total $",format(total, "10.2"),"\n\t","Discount $",format(disc,"10.2"),"\n\t","Amount Due $",format (due, "10.2"),sep="")
Upvotes: 1