Introduction to Data Science in python by University of Michigan | Coursera Community

Introduction to Data Science in python by University of Michigan

  • 22 February 2021
  • 2 replies
  • 58 views

Badge +1

Mates I am trying to solve the assignment 1 

Question : 

Part B

The dataset file in assets/grades.txt contains a line separated list of people with their grade in a class. Create a regex to generate a list of just those students who received a B in the course.?

My Solution 

import re
def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()

    # YOUR CODE HERE
    namesB=[ ]
    for item in re.finditer("(?P<name>.*)(\: B)",grades):
        namesB.append(item.group('name'))
        return namesB
    raise NotImplementedError()

grades()

 

assert len(grades()) == 16

I suppose to get 16 PEOPLE WHO GOT B in their course but it shows only 1 guy in the out put i dont understand wts wrong with my codes… Mates help plz

 


2 replies

A small detail, but clear enough - you return statement is misplaced - function returns the namesB list right after the first iteration of a loop.

Badge +1

thanks buddy I passed the assignment with 100/100 :heart_eyes::sunglasses::heart_eyes:

Reply