1

I would greatly appreciate if someone could explain why I'm getting a syntax error for the line with the else statement.

x = 0
digits = 0

while(x != 0):
  x = x // 10
  digits += 1
print(digits)

else(x == 0):
  print(1)

I have to create a code that uses a while loop and conditional statement in which it assess the number of digits an integer contains. The else loop is needed otherwise if I have x = 0, it will print 0 as the no. of digits which is of course false.

  • Your indentation of `print(digits)` is wrong, it should be on the level of the other statements inside the `while` block. It also makes no sense to have a condition on the `else` part. – walnut Oct 20 '19 at 00:51
  • to add to @uneven_mark https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement – dhuang Oct 20 '19 at 00:51
  • 1
    `else` doesn't take an expression in any case. – chepner Oct 20 '19 at 00:55
  • Think about it logically: should you handle the special case of x equal to zero *before*, or *after* the loop? (Hint: after the loop runs, is there something you can say is true about the value of x, regardless of what it was before?) – Karl Knechtel Oct 20 '19 at 02:35

2 Answers2

0

We want to know whether x == 0 first, and handle that case specially. When it's not, that's when we use the while loop, so indent all of that inside an else. Thus:

if x == 0:
    print(1)
else:
    while(x != 0):
        x = x // 10
        digits += 1
    print(digits)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
-1
x = 10000
digits = 0

while x != 0:
    x = x // 10
    digits += 1
    if x == 0:         # you only want to print once when you have digits correct so check here
        print(digits)  # print your digits
        break          # break your loop
else:   # remove the expression here for the else (not elif)
    print(1)

Looks like:

>>> x = 10000
>>> digits = 0
>>> 
>>> while x != 0:
...     x = x // 10
...     digits += 1
...     if x == 0:
...         print(digits)
...         break
... else:
...     print(1)
... 
5

Edit:

As Karl pointed out in the comments, this is not a regular way to do things in Python. This answer is this way because it seemed like you specifically wanted to use a while/else which this is the way to do that. Reorganizing it to the way he does it in his answer is much much more common, but doesn't satisfy any requirement to do while/else if that is a requirement for you.

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • This is an awkward way of doing things. The pairing of `while` with `else`, while part of the Python grammar, is unusual; and the condition for breaking the loop here (and we must conditionally break from the loop to make `else` work this way) is redundant with the `while` condition itself. – Karl Knechtel Oct 20 '19 at 02:36
  • @KarlKnechtel I don't disagree. It seemed like OP wanted the `while/else` as part of their answer which is why I kept it in there in that way. – MyNameIsCaleb Oct 20 '19 at 02:39
  • Also it isn't redundant, t is evaluating the same but it is required to be able to print the digits. It will break out before it prints otherwise. – MyNameIsCaleb Oct 20 '19 at 02:47
  • Redundant in the sense that it could equally be `while True:`, relying on the check. – Karl Knechtel Oct 20 '19 at 02:55
  • Ah I suppose that's true. It does rely on the `break` to work properly. However, if you change it to `True` then the `else` becomes the redundant part and will break it. Inherent to the issue of attempting to keep the `else` in the answer as part of the `while`. – MyNameIsCaleb Oct 20 '19 at 02:57