Python Forum
integer representing a 10-digit phone number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
integer representing a 10-digit phone number
#1
Can someone help point me in the right direction? Many thanks.

QUOTE:
Given an integer representing a 10-digit phone number, output the area code, prefix, and line number, separated by hyphens.

Ex: If the input is:

8005551212
the output is:

800-555-1212
Hint: Use % to get the desired rightmost digits. Ex: The rightmost 2 digits of 572 is gotten by 572 % 100, which is 72.

Hint: Use // to shift right by the desired amount. Ex: Shifting 572 right by 2 digits is done by 572 // 100, which yields 5. (Recall integer division discards the fraction).

For simplicity, assume any part starts with a non-zero digit. So 999-011-9999 is not allowed.
Reply
#2
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
One of the hints:

>>> 800-555-1212
-967
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
@ichabod801 - my sincere apologies...of course I completely botch up my very first post. I was hasty in my post because I was running out the door to work. Next time I will be clear about what I have tried, and what I am struggling with to be exact.

Thank you for your patience while I fumble my way through using the forum.

I was able to figure out my issue...below is my code:

phone_number = input()

print(phone_number[0] + phone_number[1] + phone_number[2] + '-' + phone_number[3] + phone_number[4] + phone_number[5] + '-' + phone_number[6] + phone_number[7] + phone_number[8] + phone_number[9])
Reply
#5
That is not going to work. Look at the assignment. You will be getting an integer. Input returns a string. The indexing you are using with raise an exception with an integer.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(May-13-2019, 08:13 PM)critter70 Wrote: @ichabod801 - my sincere apologies...of course I completely botch up my very first post. I was hasty in my post because I was running out the door to work. Next time I will be clear about what I have tried, and what I am struggling with to be exact. Thank you for your patience while I fumble my way through using the forum. I was able to figure out my issue...below is my code:
 phone_number = input() print(phone_number[0] + phone_number[1] + phone_number[2] + '-' + phone_number[3] + phone_number[4] + phone_number[5] + '-' + phone_number[6] + phone_number[7] + phone_number[8] + phone_number[9]) 

You code will work only when you enter 10 digit number. Instead you can try below

phone_number = input()
if (len(phone_number)==10):
 print(phone_number[0] + phone_number[1] + phone_number[2] + '-' + phone_number[3] + phone_number[4] + phone_number[
    5] + '-' + phone_number[6] + phone_number[7] + phone_number[8] + phone_number[9])
else:
 print('invalid phone number')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,496 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,787 May-28-2020, 09:50 AM
Last Post: bryan0901
  Four-digit number text translation. soz 3 2,650 May-13-2019, 03:02 PM
Last Post: buran
  create three digit numbers Krszt 4 4,397 Dec-09-2018, 03:12 PM
Last Post: ThePhi
  Sum of digit in a string MEH012 1 9,333 Apr-20-2018, 02:13 AM
Last Post: Larz60+
  Allow only digit penoxcz 3 3,749 Nov-14-2017, 03:04 PM
Last Post: wavic
  Four digit combinations EHod 4 7,702 Aug-13-2017, 09:14 PM
Last Post: EHod

Forum Jump:

User Panel Messages

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