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...
?)
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.
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.
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.