Need Help for Exam Function

Can someone point me in the right direction? This is a question on the first python fundamentals exam, and I am struggling. I have never used a while loop in a function with 3 parameters. Any help would be appreciated.

Can you please post your formatted code rather than a screen shot? The code is cut off on the bottom and it’s a bit difficult to read. (ie: what follows total_2 = buy_items... ?)

tarting_money = 100 starting_num_items = 10 item_price = 4 # Your code here def buy_items(): num_bought = 0 return 0 total = buy_iems(starting_money, starting_num_items, item_price) print("You were able to buy " + str(total) + " items.") # For testing purposes total_1 = buy_items(100, 10, 4) print("Test 1: " + str(total_1)) total_2 = buy_items(10, 10, 4) print("Test 2: " + str(total_2))
1 Like

There is a NameError…so, double check your spelling…after total =

Sorry I am still getting use to Codecademy. Thank you for being willing to help.

1 Like

yeah I seen that, thank you. I am just clueless on how to write the function.

Okay, so whenever you’re unsure how to write a function break the question down into smaller pieces.

Do the instructions state to create global variables? (I ask b/c maybe the instructions above are incomplete?). You want to be able to re-use the function so you can pass through any parameters. (Because the starting amount of money might change, the stock will change as will the price.) So, you really wouldn’t want to have three global variables at the top.

Your function has 3 parameters, something like this:

def buy_items(start_money, stock, price):
    num_bought = 0

They also want you to use a counter called num_bought that will give you a running total until your money runs out. So, you’ve got that in your function already…

They want you to use a while loop…which will run as long as the expression evaluates to True. Review the previous lessons on how to write a while loop. I always say if one example doesn’t make sense, then find one that does make sense to you. (there’s no one way to teach coding concepts). Sometimes it also helps if you can draw out what the question is asking too.

So, how would you write the function logic for while? Remember, the total price of all the treats cannot be more than the money you have in your pocket…(that statement has to evaluate to True.)

Your function logic isn’t quite right. I think you should possibly go back over the lesson on while loops. Your else condition will only execute when the while evaluates to False. And, if the while is never True, then the else clause will still be executed. Keep in mind that your function should work with any 3 arguments passed through it. Does it work?

  • Also, I think that starting_num_items is a bit too confusing. It would be clearer if it was called “in_stock”, right?

See the docs: https://docs.python.org/3/reference/compound_stmts.html#while

Or, here.

-Try pasting your code into the site python tutor…and see what happens.

Sometimes it helps to write out what you’re trying to do and also write out the logic of what you’ve written. This is what it says to me/how I interpret it when I plug in the numbers:

def buy_items(starting_money, starting_num_items, item_price):
  while starting_money (100) >= (starting_num_items(10) * item_price (4)): #while 100 >= 10*4 or 40:
    num_bought = starting_num_items # 4 = 4??
  else:
    num_bought = starting_money/item_price #4 = 100/4 = 25
  
  return num_bought = 25?
  • If there’s only 10 items in stock, how could one purchase 25 items?

thanks for your response! it took me some time until I got it.

1 Like
1 Like

Think about it this way and create your actions to the ‘while’ loop.

starting_money >= item_price: This condition checks if you have enough money to buy an item at the given price. If you have enough money, the loop continues. If you don’t have enough money, the loop stops.

starting_num_items > 0: This condition checks if there are still items available in the stock. If there are items available, the loop continues. If there are no more items in stock, the loop stops.

Now give yourself a chance to think about what actions are needed while the loop is running True. The logic is the hardest part and was for me.