简体   繁体   English

我正在试图弄清楚如何将括号和短划线插入到作为输出的电话号码。 我正在使用python 3.5

[英]I'm trying to figure out how to insert parentheses and a dash to a phone number that is the output. I'm using python 3.5

the first part works fine but the second part after else doesn't. 第一部分工作正常但第二部分则不然。 I can't get it to work so that the output phoneNumber from above formats into (XXX)XXX-XXXX) for example if I enter 9999999999 above I want the below to add the parentheses and and the dash. 我无法让它工作,以便输出phoneNumber从上面格式化为(XXX)XXX-XXXX)例如,如果我输入上面的9999999999我想要下面添加括号和短划线。

def main():
phoneNumber=int(input("Enter a 10 digit unformatted telephone number in the format ##########: "))
length = len("phoneNumber")
index=0


if (length ==10):
    phoneNumber=True
if phoneNumber:
    print("The unformatted number is: ",phoneNumber)

else:
    print("The telephone number was NOT entered in unformatted format ##########.")


phoneNumber= phoneNumber.insert(0,"(")
phoneNumber= phoneNumber.insert(4,")")
phoneNumber= phoneNumber.insert(8,"-")
print("The formatted number is: ",phoneNumber)


main()

The insert function is not allowed for strings and ints. 字符串和整数不允许使用insert函数。 You can use this modified function directly.: 你可以直接使用这个修改过的函数:

def main():
    phoneNumber=int(input("Enter a 10 digit unformatted telephone number in the format ##########: "))
    tempNumber = []
    phoneNumber = str(phoneNumber)
    length = len(phoneNumber)
    index=0
    print(length)


    if (length ==10):
        print("The unformatted number is: ",phoneNumber)
    else:
        print("The telephone number was NOT entered in unformatted format ##########.")

    for num in phoneNumber:     # string to list conversion by iteration
        tempNumber.append(num)

    tempNumber.insert(0,"(")
    tempNumber.insert(4,")")
    tempNumber.insert(8,"-")
    phoneNumber = ''.join(tempNumber)
    print(phoneNumber)

main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图弄清楚如何在 python 上安装这个库(时间) - I'm trying to figure out how to install this lib on python (time) 我正在尝试找出Python中的简单列表加密 - I'm trying to figure out simple list encryption in Python Python彩票生成器我正在尝试弄清楚如何添加数字输入,并将它们分隔成行,并且为x数量的num long - Python Lottery generator I'm trying to figure out how to add a number input, and separate them on lines and be x amount of num long 我是 python 的新手,我想弄清楚如何在同一行上一次打印一个字母 - I'm new to python and I am trying to figure out how to print letters one at a time on the same line 我正在试图找出如何使用带有pidgin的dbus - I'm trying to figure out how to use dbus with pidgin 我试图弄清楚如何以 2 的幂为循环递增 - I'm trying to figure out how increment for loop in powers of 2 我试图弄清楚如何计算在Python的字符串句子中字母大写的次数 - I'm trying to figure out how to count how many times a letter is capitalized in a string sentence in Python 我试图弄清楚如何让python将歌词简单地发布到歌曲中 - I'm trying to figure out how to get python to simply post the lyrics to a song 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 我正在尝试如何将此输出打印到 python 上的文本文件 - I'm trying how to print this output to a text file on python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM