#πŸ”’ while loop

24 messages Β· Page 1 of 1 (latest)

indigo otter
#

Can anyone help on this while loop I need to know why it isn't working with the elif statement

stone sentinelBOT
#

@indigo otter

Python help channel opened

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.

mint creek
#

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

indigo otter
#

Im still a little confused as to why that doesn't work out

mint creek
#

Bc elif pairs with an if

indigo otter
mint creek
#

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

indigo otter
#

yes that makes sense, thank you!

mint creek
#

Loop else actually exist to but only for for

#

!for-else

stone sentinelBOT
#
The for-else block

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.

drowsy wadi
stone sentinelBOT
mint creek
stone sentinelBOT
#
Python help channel closed

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.