Ask coding questions

← Back to all posts
Adding an ID to student name list
SakuraKimiko (4)

Hello

I am trying the following code and to do the follows in PYTHON CODE

  1. I want to start adding student by starting a blank list along with auto generated student ID i.e when once the students joins the site or program, a student no is generated e.g 001 and when the next student joins in , his or her id will become 002 meaning the ID's are generated in series.
  2. How can once create user id using python coding. e.g. once you are joining the program and they ask for personal information. Once the user himself or herself provides a user id (when asked i.e userid=input(""Your user id is:- ")). Can that ID be used to login rather than the name itself

  3. What would be the coding when two people have the same name or user id

Kindly reply all of the above questions in python (as much as possible) because currently I am only learning python.

Thank You for your time

Sakura

Commentshotnewtop
CaptainAnon (52)

You might want to use an SQL database to achieve this – they come with handy features that make life easier and are highly performant.
1. If you want to use a python list then whenever a new student is created an ID could be provisioned using len(student_list) (where student_list is the list that you're storing the student data in). It makes sense to store the student data as a dictionary, so your student list might look like:

student_list = [
{id: 1, name: 'name1', email: '[email protected]'}, # Extra fields can be added as is necessary
{id: 2, name: 'name2, email: '[email protected]'}
# And so on ...
]
  1. You can do a linear search to find the student.
userid=int(input(""Your user id is:- ")))
found_student = None
for student in student_list:
  if student.id == userid:
    found_student = student
    break
  1. You should make sure that this can't happen.
SakuraKimiko (4)

@CaptainAnon

Thank you for your prompt response, Can you kindly tell me where exactly can we apply the 1st code i.e student_list = {[ id:...}] in the following code

blank student list

student_list=[{"Name":}]

student_list= [{"":[]}]

welcome to the site

print ("***Welome***")

#ask if he or she is a student

already_student=("Are you a member:- ")

if already_student=="YES" or already_student=="yes" or already_student=="Yes" or already_student=="Y" or already_student=="y":

print ("Welcome", already_student)

else:

print ("Member not found")

do you wish to become a memeber

member=input("Do you wish to become a member:- ")

if member=="YES" or member=="yes" or member=="Yes" or member=="Y" or member=="y":

name = input("Enter your First Name:- ")

last = input("Enter your Last Name:- ")

enter age

age=int(input("Enter your age here:- "))

student id generator

greet new student and inform of their id

print ("Welcome", name, "Your student ID is ", ID)

thank you again for your time

CaptainAnon (52)

@SakuraKimiko

# create a blank list, which will be filled later
student_list = []
# ask if the student is already a member
already_member = input("Are you already a member? ")
if already_member[0].lower == "y":
  memberid = input("Please enter your name or member id: ")
  # do a search for the member
  found_student = False
  for student in student_list:
    if student['id'] == memberid:
      print("Welcome", student['name'])
      found_student = True
      break
  if not found_student:
    print("Could not find that student.")
else:
  become_member = input("Would you like to become a member? ")
  if become_member[0].lower() == "y":
    firstname = input("Please enter your first name. ")
    surname = input("Please enter your surname. ")
    age = int(input("Please enter your age. ")
    id = len(student_list)+1
    student_list.append({'firstname': firstname, surname: 'surname', 'age': age, 'id': id})
    print("You student id is", id)
SakuraKimiko (4)

@CaptainAnon

Thank you very much

You have been most helpful