lower() 函数不接受参数

问题描述 投票:0回答:2
def main():

    print("this program creates a file of usernames from a ")
    print("files of names ")
    # get the file names

    infilename = input("what files are the name in")
    outfilename = input("what file should the usernames go in")
    # open the files

    infile = open(infilename,'r')
    outfile = open(outfilename,'w')
    # process each line of the input file
    for line in infile.readlines():
        # get the first and last names from line
        first, last = line.split()
        # create the username
        uname = line.lower(first[0]+last[:7])
        # write it to the output file
        outfile.write(uname+'\n')
    # close both files
    infile.close()
    outfile.close()

    print("usernames have been written to : ", outfilename)

main()

我正在尝试编写一个程序,从文件中获取一堆名字和姓氏,然后打印一个用户名,该用户名是名字的第一个字母和姓氏的其余字母的组合。例如:

alex
doug
将是
adoug

Python 解释器在

uname = line.lower(first[0]+last[:7])
上显示错误。

TypeError lower() takes no arguments (1 given)

有没有办法解决这个错误或者有其他方法可以做到这一点?

python python-3.x lowercase
2个回答
2
投票

正确书写,相关行可能如下所示:

uname = (first[0]+last[:7]).lower()

...或者,更详细地说:

uname_unknown_case = first[0]+last[:7]
uname = uname_unknown_case.lower()

值得注意的是,用作输入的字符串是调用该方法的对象;正如错误消息所示,没有其他参数。


0
投票

下面的功能不能那样工作。如果你想在Python中将文本转换为小写,你必须执行以下操作:

string1 = "ABCDEFG"
string2 = string1.lower()
print(string2) # prints abcdefg
© www.soinside.com 2019 - 2024. All rights reserved.