Python Forum

Full Version: integer representing a 10-digit phone number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
One of the hints:

>>> 800-555-1212
-967
@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])
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.
(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')