N
N
Neal Orven2016-04-24 09:43:42
Python
Neal Orven, 2016-04-24 09:43:42

Python problem, I want to understand how it works in python, can you help?

Task:

Write a function to filter students by average grade (so that
the function returns all students above
the average grade specified in the function parameters). Approximate scheme of the function: creation of an
empty array, where all students who passed the
filtering will be added; launching a loop, in each iteration of which it is necessary to
read the average grade of the current student and compare it with the
value that is passed as a parameter.

I haven’t studied Python, please help, I don’t understand what the error is in line 29. Python version 3. I'm trying to solve this problem in ruby, so that I can somehow transfer it to python.
Initial raw code with an error:
groupmates = [
  {
    "name": "StudentOne",
    "group": "111",
    "age": 21,
    "marks": [4, 3, 5, 5, 4]
  },
  {
    "name": "StudenTwo",
    "group": "111",
    "age": 22,
    "marks": [3, 2, 3, 4, 3]
  },
  {
    "name": "StudentThree",
    "group": "111",
    "age": 23,
    "marks": [3, 5, 4, 3, 5]
  },
  {
    "name": "StudentFour",
    "group": "111",
    "age": 24,
    "marks": [5, 5, 5, 4, 5]
  }
]

def print_students(students):
  print "name".ljust(15), "group".ljust(8), "age".ljust(8), "marks".ljust(20)
  for student in students:
    print student["name"].ljust(15), student["group"].ljust(8), str(student["age"]).ljust(8), str(student["marks"]).ljust(20)
  print
    filter(lambda x: sum(x.get("marks"))/len(x.get("marks")) > 4, groupmates)

print_students(groupmates)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zelsky, 2016-04-24
@zelsky

The code below worked great
43c2d4a4bd1a4f078fd10bd527c8249c.png

groupmates = [
  {
    "name": "StudentOne",
    "group": "111",
    "age": 21,
    "marks": [4, 3, 5, 5, 4]
  },
  {
    "name": "StudenTwo",
    "group": "111",
    "age": 22,
    "marks": [3, 2, 3, 4, 3]
  },
  {
    "name": "StudentThree",
    "group": "111",
    "age": 23,
    "marks": [3, 5, 4, 3, 5]
  },
  {
    "name": "StudentFour",
    "group": "111",
    "age": 24,
    "marks": [5, 5, 5, 4, 5]
  },
  {
    "name": "StudentFive",
    "group": "111",
    "age": 29,
    "marks": [5, 5, 5, 5, 5]
  }
]

def count_mark(students,mark):
    print ("name".ljust(15), "group".ljust(8), "age".ljust(8), "marks".ljust(20))
    for student in students:
        marks_list = student['marks']
        result =  (sum(marks_list)/len(marks_list))
        if result >= need:
            print(student["name"].ljust(15), student["group"].ljust(8), str(student["age"]).ljust(8), str(student["marks"]).ljust(20))

need = int(input('Input :'))

count_mark(groupmates,need)

I
iegor, 2016-04-24
@iegor

In general, it would be worth showing an error, but what catches your eye is that in python3 print is a function, everything you want to display on the screen must be passed as an argument to this function

N
Neal Orven, 2016-04-24
@NealO

Decision:

def print_studs(students):
    t = '{:<16}{:<8}{:<8}{}'
    print(t.format('name', 'group', 'age', 'marks'))
    print('\n'.join([t.format(s['name'], s['group'], s['age'], s['marks']) for s in students]))
  
def all_students(students):
    return students
    
def students_avg(students, n):
    return [s for s in students if sum(s['marks'])/len(s['marks']) > n]

print_studs(all_students(groupmates))
print()
print_studs(students_avg(groupmates, 4))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question