#๐ Easy question, confused because im new
22 messages ยท Page 1 of 1 (latest)
@atomic phoenix
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Is it because since the first answer is true, the rest wont be executed? I cant find a solid answer and dont really trust AI. Again, thanks in advance
You are correct - since the checks are chained using elifs, each condition will only be checked if the previous one failed.
In your example the first condition is greater than 2 (which is true for an input of 1000) which causes code under the first condition to be run, while preventing the rest from executing.
think of an elif block like saying else if.
essentially meaning, that the next part(/if-block) won't run if the current one checks out; or in reverse, it'd only run when the previous one fails.
it'd go from top to buttom. However there's a flaw in this logic.
Since things larger than 500 would also be larger than 2 โ it'd run and stop at the first check (cash >= 2), hence only that (in this case) would end up executing.
unless ofc when it's less than 2 - this would end up with the else-block executing.
I wouldn't be surprised if that's to test that they understand the order the checks are done in.
Yes. It's what they chose.
Just practice use of if statements. Given they'll be used in basically any program you ever write, you should get used to them relatively quickly.
i think i got confused
Okay! So a correct way to write it would be to clarify that the next option ($15+) would not be true under the first Boolean statement?
As mentioned, the checks are done from the top to the bottom, and the first that's true is the one that's used. Think about how you can use that fact.
Alright. Thank you, it makes sense now
goto end
I'm not sure what you mean. What I was hinting at was to reverse the order of the checks. Check for larger numbers first, and then end with the sock check.
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.