-4
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()

I am trying to write a program that takes a bunch of first names and last names from a file and then print a usernames from that which is combination of the first letter from the first name and rest from the last name. Example: alex doug will be adoug.

The python interpreter show an error on uname = line.lower(first[0]+last[:7]).

TypeError lower() takes no arguments (1 given)

Is there any way around this error or is there any other way to do it?

four chan
  • 1
  • 1
  • 5
    What do you expect the `line` in `line.lower(first[0]+last[:7])` to actually mean/do? – Charles Duffy Mar 22 '17 at 21:36
  • i want to get the first letter from the first name and get the last name till the index 7 so if the name first name is four and last name is chan the result will be fchan – four chan Mar 22 '17 at 21:42
  • 2
    Yes, but that tells what you're doing with `first` and `last`, not what you're doing with the variable named `line`. Why is it part of the command `line.lower(first[0]+last[:7])`? – Charles Duffy Mar 22 '17 at 21:42
  • the line variable with the help of the loop goes through each line in the file then using the split function it splits the first name and the last name and the line that is giving the error is basically to genrate the user name – four chan Mar 22 '17 at 21:49
  • 1
    Yes, but what does that variable mean **on that specific line**? Why are you including it there? Obviously, I know what it means syntactically. I'm trying to tease out your reasoning. – Charles Duffy Mar 22 '17 at 21:50
  • To put it a different way: Yes, you're splitting the `line` variable into two different pieces. Why are you then referencing the **original**, *unsplit* variable when you're trying to combine and lower-case the pieces? – Charles Duffy Mar 22 '17 at 21:52
  • I think you need to keep reading whatever tutorial you're using. – TigerhawkT3 Mar 22 '17 at 21:56

2 Answers2

2

Correctly written, the pertinent line might look like:

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

...or, more verbosely:

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

Notably, the string to be used as input is the object on which the method is called; there are, as the error message says, no other arguments.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

The lower function does not work that way. If you want to convert text to lowercase in python, you will have to do the following:

string1 = "ABCDEFG"
string2 = string1.lower()
print(string2) # prints abcdefg
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
lordingtar
  • 1,042
  • 2
  • 12
  • 29