asked 95.2k views
4 votes
Write a function phone(num) that given a phone number as ‘1234567890’ returns the string ‘(123) 456-7890’. If num has less than 10 digits, then the function returns stars (*) in place of digits.

*In Python Code*

asked
User Demi
by
8.6k points

1 Answer

3 votes

Answer:

def phone(num):

if len(num) < 10:

return '*' * 10

else:

formatted_num = '(' + num[:3] + ') ' + num[3:6] + '-' + num[6:10]

return formatted_num

The phone function takes a num argument, which represents the phone number as a string. If the length of the number is less than 10, it returns a string of 10 asterisks (*) to indicate the insufficient digits. Otherwise, it formats the phone number as '(123) 456-7890' and returns the formatted string.

Here's an example of how you can use the function:

number = '1234567890'

formatted_number = phone(number)

print(formatted_number)

Output:

(123) 456-7890

If the number variable had less than 10 digits, the output would be:

**********

This indicates that the phone number is incomplete or insufficient.

Step-by-step explanation:

answered
User Gildas Ross
by
7.9k points