Python Forum
Wrong code in Python exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrong code in Python exercise
#1
Hi,

I am practicing Python from a learning book. When i type this code i don't get the expected result. It should return:
" Phone number found: 415-555-1011
Phone number found: 415-555-9999
Done" But i get only as a result: "done". What am i doing wrong?

def isPhoneNumber(text):
    if len(text)!=12:
        return False
    for i in range(0,3):
        if not text.isdecimal():
            return False
    if text[3]!='-':
        return False
    for i in range(4,7):
        if not text[i].isdecimal():
            return False
    if text[7]!='-':
        return False
    for i in range(8,12):
        if not text[i].isdecimal():
            return False
    return True

message='Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'
for i in range(len(message)):
    chunk=message[i:i+15]
    if isPhoneNumber(chunk):
        print('Phone number found: ' + chunk)
print('Done')
Reply
#2
There were only two things that I found that should be corrected. Line 5 should readif not text [i].isdecimal():instead ofif not text.isdecimal(): and line 21 should bechunk=message[i:i+12].
def isPhoneNumber(text):
	if len(text)!=12:
		return False
	for i in range(0,3):
		if not text [i].isdecimal():
			return False
	if text[3]!='-':
		return False
	for i in range(4,7):
		if not text[i].isdecimal():
			return False
	if text[7]!='-':
		return False
	for i in range(8,12):
		if not text[i].isdecimal():
			return False
	return True
 
message='Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'
for i in range(len(message)):
	chunk=message[i:i+12]
	if isPhoneNumber(chunk):
		print('Phone number found: ' + chunk)
print('Done')
Output:
Press ENTER or type command to continue Phone number found: 415-555-1011 Phone number found: 415-555-9999 Done
MaartenRo likes this post
Reply
#3
Thanks! That worked.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 494 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 869 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,587 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,269 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,819 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,577 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Headfirst Python Exercise GeorgeSears 3 1,407 Jun-11-2022, 06:36 PM
Last Post: GeorgeSears
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,517 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,089 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020