Mr_Pi
Mr_Pi

Reputation: 31

Try to call a function within itself

I am new to the python. Recently, I follow the online python course to learn the python. Here is a simple question from the course:

Write a function called item_order that takes as input a string named order. The string contains only words for the items the customer can order separated by one space. The function returns a string that counts the number of each item and consolidates them in the following order:

salad:[# salad] hamburger:[# hambruger] water:[# water].

The result example is: if order = "salad water hamburger salad hamburger" then the function returns

salad:2 hamburger:2 water:1

I finished the question myself without any problems. The code is:

def item_order (order):
    num_sa = 0
    num_wa = 0
    num_ha = 0

    new_order = order.split(' ')

    for item in new_order:
        if item == 'salad':
            num_sa += 1
        elif item == 'water':
            num_wa += 1
        elif item == 'hamburger':
            num_ha += 1

    group = 'salad:'+str(num_sa)+' '+'hamburger:'+str(num_ha)+' '+'water:'+str(num_wa)

    return group

print('Please enter the order.')
order = raw_input('')

group = item_order(order)
print(group)

But just curious if the input 'order' has some mistakes then how could I change my code for that situation. So I modify my code like:

def item_order (order):
    num_sa = 0
    num_wa = 0
    num_ha = 0
    flag = 0

    new_order = order.split(' ')

    for item in new_order:
        if item == 'salad':
            num_sa += 1
        elif item == 'water':
            num_wa += 1
        elif item == 'hamburger':
            num_ha += 1
        else:
            flag = 1
            break

    if flag == 1:
        print('There is something wrong on the order you insert, please re-enter.')
        fix_order = raw_input('')
        item_order(fix_order)

    group = 'salad:'+str(num_sa)+' '+'hamburger:'+str(num_ha)+' '+'water:'+str(num_wa)

    return group

print('Please enter the order.')
order = raw_input('')

group = item_order(order)
print(group)

But I find there is problem which I cannot tell where the code has bug.

Eg: if I put "salad water hamburger salad hamburge" as input 'order', it will let me to re-enter again because the 'hamburge' is wrong.

So after I re-enter the correct order "salad water hamburger salad hamburger", it will give me the result:

"salad:2 hamburger:1 water:1"

But the number of hamburger should be 2. I think there is a problem when I try to call the function item_order() within itself.

Could someone help me to find the bug?

Upvotes: 0

Views: 93

Answers (2)

mrwyatt
mrwyatt

Reputation: 193

The bug is that your function, item_order, is returning a string, but you are not capturing it anywhere. So when you call item_order in the if statement, you need to have a variable to catch the string it is returning. For example:

if flag == 1:
    print('There is something wrong on the order you insert, please re-enter.')
    fix_order = raw_input('')
    group = item_order(fix_order)
    return group

group = 'salad:'+str(num_sa)+' '+'hamburger:'+str(num_ha)+' '+'water:'+str(num_wa)

return group

This would fix your problem by returning the output of the item_order function on the fix_order input.

Upvotes: 1

melpomene
melpomene

Reputation: 85757

if flag == 1:
    print('There is something wrong on the order you insert, please re-enter.')
    fix_order = raw_input('')
    item_order(fix_order)

This item_order call has no effect. You're not using the return value.

Remember that calling a function doesn't just jump to the start of the function - after the function returns, execution continues where the call happened.

Which in your case is this line:

group = 'salad:'+str(num_sa)+' '+'hamburger:'+str(num_ha)+' '+'water:'+str(num_wa)

These are still the variables from the original invalid order.


The easiest way to fix this code is to change

    item_order(fix_order)

to

    return item_order(fix_order)

That way you're ignoring the broken input and the num_* counts accumulated so far; instead you're forwarding the result of the fixed order to your own caller.

Upvotes: 1

Related Questions