Home > Back-end >  Creating emails while accounting for two first names when using input()
Creating emails while accounting for two first names when using input()

Time:02-24

My code below works as long as all of the student's name consists of only two names - ex. Julie Andrews. But, when generating the student's emails, I'm trying to account for the students who have two first names - ex. Mary Jane Stewart. I want it to output something like [email protected], vs. what my current code will print, which is [email protected] - totally ignoring the student's last name.

After hours of researching Google, I have tried updating my create_emails fx to change my original variable first_last = name.split(" ") to something like first, middle, last = name.split(" ") or first_last = name.split(" ", 2) while also, respectively, updating the line utilizing the attribute .append from its original to student_emails.append(first_last[0][0] first_last1 first_last[2] last_three_sid "@gmail.com") or student_emails.append(first[0] middle[0] last last_three_sid "@gmail.com"). All attempts have obviously returned some form of an error...

The attached Stack Overflow article is the closest thing I could find whose logic might be applicable to what I'm trying to accomplish here, specifically the comment by Manfred, but in reading it, I don't know how to apply what they've done to my program... because I don't quite understand what it is that I'm reading... since I'm such a newbie at all this. I'd appreciate any help you can offer.

student_names = []

def create_names():
  count = 1
  while count <= 5:
    name = input("Enter student name, please. ")
    student_names.append(name)
    count  = 1 
create_names()

import random

student_ids = []

def create_ids():
  student_id = random.randint(111111,999999)
  return student_id

def create_id_list():
  for name in student_names:
    student_ids.append(create_ids())
create_id_list()

student_emails = []

def create_emails():
  for name in student_names:
    first_last = name.split(" ")
    sid = str(student_ids[student_names.index(name)])
    len_sid = len(sid)
    last_three_sid = sid[len_sid-3:len_sid]
    student_emails.append(first_last[0][0]   first_last[1]   last_three_sid   "@gmail.com") #ignores last index if one is provided.

create_emails()

def student_info():
  for name in student_names:
    name_pos = student_names.index(name)
    print("\n"   "name: "   name)
    print("id: "   str(student_ids[name_pos]))
    print("email: "   student_emails[name_pos])
student_info()

Finding and first and middle initials in a list of names in python

CodePudding user response:

You could do it like this I guess:

student_names = []

def create_names():
    count = 1
    while count <= 5:
        name = input("Enter student name, please. ")
        student_names.append(name)
        count  = 1 
create_names()

import random

student_ids = []

def create_ids():
    student_id = random.randint(111111,999999)
    return student_id

def create_id_list():
    for name in student_names:
        student_ids.append(create_ids())
create_id_list()

student_emails = []

def create_emails():
    for name in student_names:
        email_name = ""
        first_last = name.split(" ")
        for i, v in enumerate(first_last):
            if i > len(first_last)-2:
                break
            email_name =v[0]
        email_name = email_name first_last[-1]
        sid = str(student_ids[student_names.index(name)])
        len_sid = len(sid)
        last_three_sid = sid[len_sid-3:len_sid]
        student_emails.append(email_name   last_three_sid   "@gmail.com") #ignores last index if one is provided.

create_emails()

def student_info():
    for name in student_names:
        name_pos = student_names.index(name)
        print("\n"   "name: "   name)
        print("id: "   str(student_ids[name_pos]))
        print("email: "   student_emails[name_pos])
student_info()

Result:

Enter student name, please. Julie Andrews
Enter student name, please. Mary Jane Stewart
Enter student name, please. Jack Hendricks
Enter student name, please. Maria Basset Juliett
Enter student name, please. Marco Hansen

name: Julie Andrews
id: 742536
email: [email protected]

name: Mary Jane Stewart
id: 823274
email: [email protected]

name: Jack Hendricks
id: 590875
email: [email protected]

name: Maria Basset Juliett
id: 982168
email: [email protected]

name: Marco Hansen
id: 671240
email: [email protected]

CodePudding user response:

The code is too redundant and could be implemented with some simpler structures.

  1. name construction can use split combined with join function
  2. storage can also use dictionaries to store multiple information about the same object, compared to multiple lists so more concise and efficient The code itself is not a big problem, mainly the syntax structure requires more skilled,let's encourage each other in our endeavours try this:

import random

info = []

# You can use for loop if you already know how many times it will loop
for i in range(5):
    name = input("Enter student name, please:")  # get name
    student_id = random.randint(111111, 999999)  # get id
    name_structure = name.split()
    email = "{name_abbr}{last_name}{sid}@gmail.com".format(
        name_abbr="".join([item[0] for item in name_structure[:-1]]),   # Generate initials
        last_name=name_structure[-1],   # Generate the last part of the name
        sid=str(student_id)[-3:]    # Generate the id in the mailbox
    )
    info.append({"name": name, "id": student_id, "email": email})       # Store to the list, or print directly

# Print Information
for item in info:
    print("name:", item["name"])
    print("id:", item["id"])
    print("email:", item["email"])
    print()
  • Related