#π while loop
24 messages Β· Page 1 of 1 (latest)
@indigo otter
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.
Bc theres no if to pair it with
If u want the while to be the if u just put an if after the while outside of it
If u want to return to the loop after the else perhaps u should use a while True with an if an elif and an else containing break
Also not = but ==
I think what u want here is to put the print outside of the while and remove the elif
That's what i initally saw in the tutorial, but I was just confused as to why the method I did just didn't work
Im still a little confused as to why that doesn't work out
Bc elif pairs with an if
is that always the case?
Also it should be on the same level as the if it pairs with
So even if it would work it ahould be outside of the loop
And if it is than the else part is no longer needed
ahhhhhh
yes that makes sense, thank you!
In Python it's possible to attach an else clause to a for loop. The code under the else block will be run when the iterable is exhausted (there are no more items to iterate over). Code within the else block will not run if the loop is broken out using break.
Here's an example of its usage:
numbers = [1, 3, 5, 7, 9, 11]
for number in numbers:
if number % 2 == 0:
print(f"Found an even number: {number}")
break
print(f"{number} is odd.")
else:
print("All numbers are odd. How odd.")
Try running this example but with an even number in the list, see how the output changes as you do so.
!e it exists for both kinds of loops:py x = 20 while x > 10: x -= 1 else: print('While loop exited naturally')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
While loop exited naturally
I was thinking it may but only known for for
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.