Home > front end >  Create function to see which students failed the course [duplicate]
Create function to see which students failed the course [duplicate]

Time:09-26

I started learning Python and playing around with functions.

students = ["Peter", "Mary", "George", "Emma"]
student_ages = [24, 25, 22, 20]
student_gpa = [3.1, 3.8, 2.5, 2.0]
student_fail = 3.0

students_dict_comp = {name:[age,gpa] for name, age, gpa in zip(students, student_ages, student_gpa)}
print(students_dict_comp)

def students_failed (students): 
  for student in students_dict_comp: 
    if student_gpa >= student_fail:
      print("Student passed")
    else:
      print("Student failed")

When I call the function I need to know the names but what if I don't know them and I want an overview over the students? How can I see who failed?

CodePudding user response:

def students_passed():  
  for student in students_dict_comp: 
    if student_gpa <= student_fail:
      print(f"{student} failed")
    else:
      print(f"{student} passed")

CodePudding user response:

You don't need to know the names. As you can see, the list of names students you are passing to the function isn't used by the function.

Instead, the name of each student is already available as the variable student because of the loop for student in students_dict_comp.

You can just include it in the print output:

def students_failed():   # "students" is not needed here
  for student in students_dict_comp: 
    if student_gpa >= student_fail:
      print(f"{student} passed")
    else:
      print(f"{student} failed")

By using an f-string (f" ... "), variables inside {} are automatically replaced by their string value.

CodePudding user response:

You will get the student name in the student variable that you used in for loop. Then, to access a student's values you have to use your dictionary with a key to get it.

For example:
if you print variable students_dict_comp, it will look like:
{'Peter': [24, 3.1], 'Mary': [25, 3.8], 'George': [22, 2.5], 'Emma': [20, 2.0] }
Now, to access Mary's information, you have to write:

students_dict_comp['Mary'] # [25, 3.8] 

# or if we use another variable to save name

student = 'Mary'
student_info = students_dict_comp[student] # [25, 3.8]

if we print it, we will get a list which is [25, 3.8]. Now, if we want to access his age or CGPA, we can get it like below:

marry_info = students_dict_comp['Mary'] # [25, 3.8]
marry_age = marry_info[0]
marry_cgpa = marry_info[1]

# or we can directly access

mary_age = students_dict_comp['Mary'][0] # age in 0th position
mary_cgpa = students_dict_comp['Mary'][1] # cgpa in 1th position

In the for loop we got the each student name in the student variable. So, if we want to access their name, we can just use student variable. Otherwise, if we want to access the student age or cgpa, then we can access from dict like this:

student_gpa = students_dict_comp[student][1]

#another way
student_info = students_dict_comp[student] # an array with two value
student_gpa = student_info[1] # cgpa in 1 no. position.

where students_dict_comp[student] will give the student information which is an array with two values. Then, to get the CGPA we have to access 1 no. position.
After that, if we print the student variable, we will get the student name.

Full code with solution will be:

students = ["Peter", "Mary", "George", "Emma"]
student_ages = [24, 25, 22, 20]
student_gpa = [3.1, 3.8, 2.5, 2.0]
student_fail = 3.0

students_dict_comp = {name:[age,gpa] for name, age, gpa in zip(students, student_ages, student_gpa)}
print(students_dict_comp)

def students_failed (students_dict_comp): 
    for student in students_dict_comp:
        #  print(student) # Peter, Mary, George, Emma
        student_gpa = students_dict_comp[student][1]
        if student_gpa >= student_fail:
            print(student, " passed")
        else:
            print(student, " failed")
students_failed(students_dict_comp)
  • Related