I suck at tests
from random import choice class Student: def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name self.grades = [] @property def full_name(self): return f'{self.first_name} {self.last_name}' @property def email(self): return f'{self.first_name}{self.last_name[0]}@codewars.com' def add_grade(self, grade): self.grades.append(grade) @property def grade_average(self): if not self.grades: return None return sum(self.grades) / len(self.grades) def assess(self): if not self.grades: return None avg = self.grade_average if avg >= 90: return 'A' elif avg >= 80: return 'B' elif avg >= 70: return 'C' elif avg >= 65: return 'D' else: return 'F' def add_random_grade(self): random_grade = choice(range(1, 11)) self.grades.append(random_grade)
- from random import choice
- class Student:
def __init__(self, first_name, last_name, grades=[]):- def __init__(self, first_name, last_name):
- self.first_name = first_name
- self.last_name = last_name
self.grades = grades- self.grades = []
- @property
- def full_name(self):
- return f'{self.first_name} {self.last_name}'
- @property
- def email(self):
- return f'{self.first_name}{self.last_name[0]}@codewars.com'
- def add_grade(self, grade):
- self.grades.append(grade)
- @property
- def grade_average(self):
- if not self.grades:
- return None
- return sum(self.grades) / len(self.grades)
@propertydef basic_grades(self):return (('A', 90), ('B', 80), ('C', 70), ('D', 65))- def assess(self):
- if not self.grades:
- return None
- avg = self.grade_average
for grade in self.basic_grades:if avg >= grade[1]:return grade[0]return 'F'- if avg >= 90:
- return 'A'
- elif avg >= 80:
- return 'B'
- elif avg >= 70:
- return 'C'
- elif avg >= 65:
- return 'D'
- else:
- return 'F'
- def add_random_grade(self):
- random_grade = choice(range(1, 11))
- self.grades.append(random_grade)
from random import choice def test_full_name(): student = {"first_name": "John", "last_name": "Doe", "grades": []} full_name = full_name(student) assert full_name == "John Doe", f'Expected "John Doe", but got {full_name}' def test_email(): student = {"first_name": "John", "last_name": "Doe", "grades": []} email = email(student) assert email == "jdoe@codewars.com", f'Expected "jdoe@codewars.com", but got {email}' def test_add_grade(): student = {"first_name": "John", "last_name": "Doe", "grades": []} add_grade(student, 85) assert student["grades"] == [85], f'Expected [85], but got {student["grades"]}' def test_grade_average(): student = {"first_name": "John", "last_name": "Doe", "grades": [85, 90, 95]} grade_average = student_grade_average(student) assert grade_average == 90, f'Expected 90, but got {grade_average}' def test_assess(): student = {"first_name": "John", "last_name": "Doe", "grades": [85, 90, 95]} assessment = assess(student) assert assessment == 'A', f'Expected "A", but got {assessment}' def test_add_random_grade(): student = {"first_name": "John", "last_name": "Doe", "grades": [85, 90, 95]} add_random_grade(student) assert len(student["grades"]) == 4, f'Expected 4 grades, but got {len(student["grades"])}'
from solution import Student- from random import choice
- def test_full_name():
- student = {"first_name": "John", "last_name": "Doe", "grades": []}
- full_name = full_name(student)
- assert full_name == "John Doe", f'Expected "John Doe", but got {full_name}'
@test.describe("Student")def test_group():s1 = Student('niccolò', 'machiavelli', [100, 100, 100, 100])s2 = Student('hannibal', 'lector', [100, 100, 99, 98])s3 = Student('julius', 'caesar', [93, 89, 86, 91])s4 = Student('jack', 'sparrow', [71, 81, 78, 75])s5 = Student('forest', 'gump', [65, 75, 70, 69])s6 = Student('jarjar', 'binks', [10, 3, 1, 0])@test.it("First Name")def first_name_test():samples = [(s1, 'niccolò'), (s2, 'hannibal'), (s3, 'julius'), (s4, 'jack'), (s5, 'forest'), (s6, 'jarjar')]for sample in samples:test.assert_equals(sample[0].first_name, sample[1])@test.it("Last Name")def first_name_test():samples = [(s1, 'machiavelli'), (s2, 'lector'), (s3, 'caesar'), (s4, 'sparrow'), (s5, 'gump'), (s6, 'binks')]for sample in samples:test.assert_equals(sample[0].last_name, sample[1])@test.it("Full Name")def first_name_test():samples = [(s1, 'niccolò machiavelli'), (s2, 'hannibal lector'), (s3, 'julius caesar'), (s4, 'jack sparrow'), (s5, 'forest gump'), (s6, 'jarjar binks')]for sample in samples:test.assert_equals(sample[0].full_name, sample[1])@test.it("Email Address")def first_name_test():samples = [(s1, 'niccolòm@codewars.com'), (s2, 'hanniball@codewars.com'), (s3, 'juliusc@codewars.com'), (s4, 'jacks@codewars.com'), (s5, 'forestg@codewars.com'), (s6, 'jarjarb@codewars.com')]for sample in samples:test.assert_equals(sample[0].email, sample[1])@test.it("Student Grades")def first_name_test():samples = [(s1, 'A'), (s2, 'A'), (s3, 'B'), (s4, 'C'), (s5, 'D'), (s6, 'F')]for sample in samples:test.assert_equals(sample[0].assess(), sample[1])@test.it("Random Grades")def first_name_test():number_of_grades = len(s1.grades)for i in range(10):s1.add_random_grade()test.assert_equals(number_of_grades := number_of_grades + 1, len(s1.grades))- def test_email():
- student = {"first_name": "John", "last_name": "Doe", "grades": []}
- email = email(student)
- assert email == "jdoe@codewars.com", f'Expected "jdoe@codewars.com", but got {email}'
- def test_add_grade():
- student = {"first_name": "John", "last_name": "Doe", "grades": []}
- add_grade(student, 85)
- assert student["grades"] == [85], f'Expected [85], but got {student["grades"]}'
- def test_grade_average():
- student = {"first_name": "John", "last_name": "Doe", "grades": [85, 90, 95]}
- grade_average = student_grade_average(student)
- assert grade_average == 90, f'Expected 90, but got {grade_average}'
- def test_assess():
- student = {"first_name": "John", "last_name": "Doe", "grades": [85, 90, 95]}
- assessment = assess(student)
- assert assessment == 'A', f'Expected "A", but got {assessment}'
- def test_add_random_grade():
- student = {"first_name": "John", "last_name": "Doe", "grades": [85, 90, 95]}
- add_random_grade(student)
- assert len(student["grades"]) == 4, f'Expected 4 grades, but got {len(student["grades"])}'