Reputation: 45
I am getting an error in my program that says finddiscount() missing one required positional argument. I can't seem to figure out how to properly code the find discount function could someone please help? Im sure this is a simple error but I am new to python and especially new to functions. Thanks!
def finddiscount(discount):
if quantity >= 1 and quantity <= 9:
discount = "0%"
elif quantity >= 10 and quantity <= 19:
discount = "20%"
elif quantity >= 20 and quantity <= 49:
discount = "30%"
elif quantity >= 50 and quantity <= 99:
discount = "40%"
elif quantity >= 100:
discount = "50%"
print (discount)
def main():
quantity = int(input("How many packages where purchased?"))
price = float(input("How much is each item?"))
finddiscount()
return
def finddiscount(quantity):
if quantity >= 1 and quantity <= 9:
discount = "0%"
elif quantity >= 10 and quantity <= 19:
discount = "20%"
elif quantity >= 20 and quantity <= 49:
discount = "30%"
elif quantity >= 50 and quantity <= 99:
discount = "40%"
elif quantity >= 100:
discount = "50%"
print (discount)
main()
Upvotes: 0
Views: 612
Reputation: 56654
You can avoid the cascade of if statements (and unexpected errors - what if quantity = 0? or 110?) like so:
import bisect
import sys
inp = raw_input if sys.hexversion < 0x3000000 else input
def type_getter(type_):
def getfn(prompt):
while True:
try:
return type_(inp(prompt))
except ValueError:
pass
return getfn
get_int = type_getter(int)
get_float = type_getter(float)
discount_points = [ 0, 10, 20, 50, 100]
discount_fractions = [.0, .0, .2, .3, .4, .5]
def discount_fraction(qty):
ndx = bisect.bisect_right(discount_points, qty)
return discount_fractions[ndx]
def main():
qty = get_int("How many were purchased? ")
price = get_float("How much is each?")
frac = discount_fraction(qty)
total = price * (1. - frac) * qty
print("Final cost is ${:0.2f}.".format(total))
if __name__=="__main__":
main()
Upvotes: 0
Reputation: 365845
You define the function as taking one parameter:
def finddiscount(discount):
(and later redefine it, but let's ignore that for now, since the second definition also takes one parameter).
But then you call it with no arguments:
finddiscount()
Presumably you wanted this:
finddiscount(quantity)
There are a number of other problems with your code. You shouldn't be defining all of this stuff inside a finddiscount
function definition. And you definitely shouldn't be defining a local function named finddiscount
inside a global function with the same name.
Also, you usually want your functions to actually return
something, not just print
a value and then do nothing. (For example, you might want to return
the discount, so some later code can apply that discount to the price
.)
But this will solve the problem you asked about.
Upvotes: 1
Reputation: 239523
You are invoking the function like this
finddiscount()
But it is defined like this
def finddiscount(quantity):
So, you should be passing a value for the quantity
parameter.
finddiscount(quantity)
Upvotes: 3