What They Never Told You About Programming - Part 2

Xavier works at a sporting goods shop in the online sales department, and he is debugging the code below, which is designed to call the purchase_items() function until all desired items have been bagged. What is wrong with the code?

1
2
3
4
5
purchased=0
quantity_desired = 10

while purchased < quantity_desired:
    purchase_items()

It will not ever call the purchase_items() function It will not run at all It will purchase too many items It will purchase too few items

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

6 solutions

Discussions for this problem are now closed

Daniel Lim
Mar 31, 2014

Staff Update : The problem was incorrectly phrased because it used the wrong variable. The problem has since been updated. For those who attempted the problem before April 3rd 10AM PST, we marked "It will not ever call the purchase_items() function" as correct" (even though it will say "The correct answer is It will purchase too many items ".)

Daniel's solution:

Infinite loop!

purchased never increases

should be

1
2
3
4
5
purchased=0
quantity_desired=10
while purchased < quantity_desired:
    bag_item()
    purchased += 1 

what if the purchased variable is incremented in the purchase_items() ?

Pallavi Dhotre - 7 years, 2 months ago

if it is incremented, the loop will eventually terminate when the condition is satisfied

Daniel Lim - 7 years, 2 months ago

there is no argument passing to the purchase_items().

Rizwan Khawaja - 7 years, 2 months ago

what saddened me is I chose the "It will purchase too many items" and the page script didnt work so I reload the page and click the option on the same spot. never knew the positions of the options are changing. well thats the first and last.

Roi Vinson Abrazaldo - 7 years, 2 months ago
Eddie The Head
Mar 30, 2014

Clearly it forms an infinite loop due to lacking constraints and hence will purchase too many items

infinite loop as purchased does not increase

Salman Dar - 7 years, 2 months ago

It has the constaint "while purchased < quantity_desired", so it doesn't "lack constraints".

Chung Kevin - 7 years, 2 months ago

The reason is because purchased item was not incremented in the code. The code above creates an infinite loop.

Daniel Anyidoho - 7 years, 2 months ago

Infinite loop. The number of purchased items never increase

Sachin Malhotra - 7 years, 2 months ago
Ivan Koswara
Apr 2, 2014

As currently stated, the problem lacks enough information to determine whether the code is correct (it might be possible that in bag_item the global variable purchased is incremented). However, the most reasonable assumption is that bag_item doesn't mess with these global variables, and thus the values of purchased and quantity_desired will never change during the loop, and hence the inequality purchased < quantity_desired will always be true, keeping the while loop running forever. Thus it will "purchase" "too many" items.

Also, apparently for some reason the less than sign (<) is converted into &amp;lt; inside a code tag...

Exactly. It is common practise to do such changes in the function itself, for the sake of modularity.

Prashant Sinha - 7 years, 2 months ago
Shaikh Shiku
Apr 3, 2014

infinite loop! purchased variable must be updated using purchased++ statement.

as i see it's a python code, python doesn't support unary increment operation !

Syed Anik - 7 years, 2 months ago
Paul Paul
Jul 27, 2014

Won't modify the variable purchased so the loop won't end.

Sanjay Kamath
Apr 4, 2014

equivalent to : while (true) -> with no exit condition

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...