CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine all parts into a working app.

Putting it together: solve with a function

In the final lesson you will combine everything into a hesapla function. This function takes a grades list and returns its average:

def hesapla(lst):
    return sum(lst) / len(lst)

print("final:", hesapla(grades))
# final: 87.0

The starting code includes all previous solutions. You will define and call the hesapla function. This makes the code modular — the function can be reused with different grade lists.

The project is complete!

Practice Define and call the hesapla function

Define def hesapla(lst): with return sum(lst) / len(lst) in the body. Then write print("final:", hesapla(grades)).

Starter code

main.py
# Grade calculator - grades list
grades = [85, 90, 78]
print(grades)
grades.append(95)
print("eklendi:", len(grades))
average = sum(grades) / len(grades)
print("ortalama:", average)
empty = []
try:
    result = sum(empty) / len(empty)
except ZeroDivisionError:
    print("boş liste")
else:
    print(result)
# define and call the hesapla function

Sample solution

main.py
grades = [85, 90, 78]
print(grades)
grades.append(95)
print("eklendi:", len(grades))
average = sum(grades) / len(grades)
print("ortalama:", average)
empty = []
try:
    result = sum(empty) / len(empty)
except ZeroDivisionError:
    print("boş liste")
else:
    print(result)
def hesapla(lst):
    return sum(lst) / len(lst)
print("final:", hesapla(grades))

Expected output

[85, 90, 78]
eklendi: 4
ortalama: 87.0
boş liste
final: 87.0

Project complete

Congratulations! You completed the five-lesson beginner final. You combined:

  • List for storing data
  • append for dynamic insertion
  • sum / len for computing the average
  • try/except for error handling
  • function for modular code

These five layers form the skeleton of a real app. In the next level OOP, decorators and more advanced topics will follow.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match the project assembly

Term Meaning
def hesapla Function definition
return sum / len Average
hesapla(grades) Call
print("final:") Output

2 Complete the return statement

Question
def hesapla(lst):
    ____
Answer
def hesapla(lst):
    return sum(lst) / len(lst)

Explanation: return sum(lst) / len(lst) returns the average.

3 What does hesapla(grades) return? (grades = [85, 90, 78, 95])

  • 348
  • 87.0 Correct answer
  • 4
  • 85

Explanation: sum([85,90,78,95]) / len = 348 / 4 = 87.0.

All lessons in Beginner