Algorithms: Creating Usernames

usernamesThe following starter code is loosely based on the flowchart shown. The function provided is used to check whether a username already exists.

Use the starter code to implement the sections marked #TODO.

The text file, with usernames can be downloaded here.

 

 

'''
creating usernames
uses a file of existing usernames
this file is read in and then new usernames are checked against it
complete sections marked #TODO
'''

# reading in existing usernames
def read_usernames():
    with open('usernames.csv') as ufile:
        data = ufile.readlines()
    
    usernames = []
    for line in data:
        line = line.strip().split(',')
        usernames.append(line[4]) # only appends usernames, rest discarded
    
    return usernames

# checking if username exists
# the if condition in this function is equivalent to the one-line return    
def exists(username):
    '''
    if username in read_usernames():
        return True
    '''    
    return username in read_usernames()

# this writes the data back to the file
def write_userdata(udata):
    with open('usernames.csv', 'a') as ufile:
        ufile.write(udata)
        
    print(udata, 'has been written to the file.')
    print('Open the file: "usernames.csv" to see the data in the file')

    
#TODO write your code to construct a username below
#use the exists() function to check if a username already exists





#TODO write the code to construct a comma separated string of the format:
# yearofentry, firstname, surname, initial, username\n

 


#TODO call the function write_userdata() with your string



Leave a Reply

Your email address will not be published. Required fields are marked *