def PhoneNumber(text):
if len(text) != 14:
return False
for i in range(0, 4):
if not text[i].isdecimal():
return False
if text[4] != '-':
return False
for i in range(5, 8):
if not text[i].isdecimal():
return False
if text[8] != '-':
return False
for i in range(9, 11):
if not text[i].isdecimal():
return False
if text[11] != '-':
** for i in range(12, 14):
if not text[i].isdecimal():
return False **
return True
Asked
Active
Viewed 58 times
0
-
why you typed "**" in your code? are you trying to make it comment? – deadshot Jul 04 '20 at 12:54
-
What is the purpose of using `**` in your example? – errata Jul 04 '20 at 12:54
-
1Hello Mag, maybe you are mixing spaces and tabs in your code? – Guillaume Jacquenot Jul 04 '20 at 12:56
-
There's an indentation error because it isn't (as shown) indented. – chepner Jul 04 '20 at 12:57
-
1Also, all those `for` loops can be replaced with `if not all(c.isdecimal() for c in text[0:4]): return False` and so on, or the _entire method_ can be replaced with checking a regular expression like `^\d{4}-\d{4}-\d[3}-\d{3}$` – tobias_k Jul 04 '20 at 13:01
-
Aside from the indentation error you get, this can be done in a single line using regex instead of `if`s and `for`s. – taha Jul 04 '20 at 13:01
-
See [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/q/45621722/3890632) – khelwood Jul 04 '20 at 13:14
-
@errata I was clueless about how to highlight the piece of code I needed to fix so that's the reason why I applied stars** – Adam Jul 04 '20 at 13:57
-
@deadshot no, trying to highlight the piece of code I wanted to correct. – Adam Jul 04 '20 at 14:05
-
@GuillaumeJacquenot I checked and there's nothing to do with that, just needed to empty the if statement with [pass] – Adam Jul 04 '20 at 14:10
1 Answers
2
Just before the for loop you have the following:
# This if block is empty
if text[11] != '-':
for i in range(12, 14):
if not text[i].isdecimal():
return False
return True
In python, empty code blocks have to use the pass
keyword
So, just add an indented pass
to the empty if statement
# That's better :)
if text[11] != '-':
pass
for i in range(12, 14):
if not text[i].isdecimal():
return False
return True

thesilican
- 581
- 5
- 17
-
4If the `if` is intended to be empty, it can just be eliminated altogether. `pass` is usually used to ignore a case matched by an `elif` to eliminate falling through to an `else` clause. – chepner Jul 04 '20 at 12:58
-
-
@Mag no problem as it's your first time, welcome to stack overflow! :) – thesilican Jul 04 '20 at 13:02
-
@Mag For future questions make sure to learn proper formatting rules - you're more likely to get responses with a thoughtful, well-formatted question – thesilican Jul 04 '20 at 13:13
-