newcodic2018
newcodic2018

Reputation: 31

How to pass assertion at this stage?

I created this function that iterates through dictionaries. I have a price dict, with each price of an item is stored. then customer order dictionary, where every order per customer is stored.

for every item in a customer order, I multiplied the item to the price, for example...

order 1: 10 books * $10.0

in the end of this function, if the total order is above $100 I will give 10% discount. 5% for above $50 and no discount if lesser than $50.

Now, for some reasons, I cannot alter assertion syntax below..Have to stick with the format and codes in there, the problem is I'm getting an assertion error. which supposed to be, the final output is "DONE"

How can I avoid catching assertions at this stage?

In particular im getting assertion error in order1

here's what I've done...

def calculate_price(price, order):
    final_list = []
# Iterating through each dictionary key.
    for key, order_value in order.items():
    # Splitting on whitespace, taking the first result.
        first_word = key.split()[0]
    # Initiating condition to compare key similarities.
        if first_word in price:
        # Matching dict keys successful.
            price_value = price[first_word]
            # Multiplying key-pair values of two matched keys.
            individual_price = (order_value*price_value)
            final_list.append(individual_price)
    new = sum(final_list)
    if new >= 101:
        order3 = new - (new*0.10)
    elif new >= 51:
        order1 = new - (new*0.05)
        order1 = int(order1)
    else:
        order2 = new

price = {'book': 10.0, 'magazine': 5.5, 'newspaper': 2.0}

order1 = {'book': 10}
order2 = {'book': 1, 'magazine': 3}
order3 = {'magazine': 5, 'book': 10}

assert(95 == calculate_price(price, order1))
assert(26.5 == calculate_price(price, order2))
assert(114.75 == calculate_price(price, order3))
print("Done")

your suggestions and help are very much appreaciated. thank you

Upvotes: 0

Views: 306

Answers (2)

passionatedevops
passionatedevops

Reputation: 539

Try this :

Also added code for raising keyerror in case item in order does not exist in price dictionary.

def calculate_price(price, order):
    count = 0
    for i in order:
        if i in price:
            count += (price[i] * order[i])
        else :
            raise KeyError('Key not found')
        
    if 50 <= count <= 100:
        count *= 0.95
    elif count > 100:
        count *= 0.90

    return count

price = {'book': 10.0, 'magazine': 5.5, 'newspaper': 2.0}

order1 = {'book': 10}
order2 = {'book': 1, 'magazine': 3}
order3 = {'magazine': 5, 'book': 10}

assert(95 == calculate_price(price, order1))
assert(26.5 == calculate_price(price, order2))
assert(114.75 == calculate_price(price, order3))
print("Done")

Upvotes: 0

David Murphy
David Murphy

Reputation: 21

https://www.tutorialspoint.com/python/assertions_in_python.htm

When it encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, Python raises an AssertionError exception.

In your code, the function evaluated to false because your calculate_price function returns a None value.

The contract implied in the assert statement is that the function will return an int or float, that is the cost value calculated for a single order from the inputs to the function.

Upvotes: 1

Related Questions