Question: This is the problem I am working on. I have a code started but it needs some fixing... You're enjoying learning programming so much, you've

This is the problem I am working on. I have a code started but it needs some fixing...

You're enjoying learning programming so much, you've decided to run your own class teaching programming basics. You've got plenty of students signing up, and now you need to make logins for each of them. Since usernames (but not first names) have to be unique, you decide to write a program to help generate them.

You should read in the classlist.txt which contains on each line. Your program should print out a list of usernames based on the following rules:

Username should be a students' first and, if applicable, middle name(s), with no spaces, converted to lower case.

If there already exists a student with that username, use their firstname plus first letter of last name converted to lower case.

If that already exists, use firstname plus first letter of lastname, plus a number, starting at 1 and counting up as required.

For example, given the following classlist.txt:

Max Eisenhardt Katherine Pryde Anna Marie Darkholme James Howlett James Proudstar James Prindle James Bradley

then your program should print out:

max katherine annamarie james jamesp jamesp1 jamesb

You can print out the final list in the same ordering as the original file.

Here is another example. Given the following classlist.txt:

Sam Vimes Sam Flynn Sam Gamgee Sam Carter Sam Velo Sam Victory Sam Vega

then your program should print out:

sam samf samg samc samv samv1 samv2

This is the code I have... but it print samv1, samv1 instead of samv1 samv2 for the last part

usernames = [] for line in open('classlist.txt'): count = 1 name = line.lower().split() surname = name[-1] username = ''.join(name[0:-1]) username_w_surname = username + surname[0] username_w_surname_integer = username_w_surname + str(count) if username not in usernames: usernames.append(username) elif username in usernames: if username_w_surname in usernames: count = count + 1 usernames.append(username_w_surname_integer) else: usernames.append(username_w_surname) for entry in usernames: print (entry)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!