Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Customizing the Django Admin Customizing the List View Test: Custom Attributes

Tobias Mahnert
Tobias Mahnert
89,414 Points

Stuck with this Code Challenge

Stuck... Who can help me out or give me a useful tipp?

Add a method to the Quiz model, named number_correct_needed that returns a string showing the number of correct answers needed to get at least 70% on the quiz. Your string should include the number needed and the total number. An example would be "7/10". Use the ceil function in the math module which has already been imported for you.

from django.db import models
import math 

class Step(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    order = models.IntegerField(default=0)

    class Meta:
        abstract = True
        ordering = ['order',]

    def __str__(self):
        return self.title

class Quiz(Step):
    total_questions = models.IntegerField()
from django.contrib import admin 

from . import models

class QuizAdmin(admin.ModelAdmin):
    list_display = ['title', 'course']

2 Answers

Tobias Mahnert
Tobias Mahnert
89,414 Points

Never Mind I just solved it and I'd like to share my solutionas others may stuck and get frustrated as well. This is for task one

    def number_correct_needed(self):
      total = self.total_questions
      needed = ((total/100)*70)

      return "{}/{}".format(math.ceil(needed), math.ceil(total))
Erika Suzuki
Erika Suzuki
20,299 Points

So, this means, I would have to study math in order for me to complete this challenge by myself.

Øyvind Andreassen
Øyvind Andreassen
16,839 Points

This is also a valid solution. No need to ask for ceil on the total number of questions, since we can presume that a quiz don't have 4.5/6.7/14.2/ect. questions.

    def number_correct_needed(self):
        return "{}/{}".format(math.ceil(self.total_questions*0.7), self.total_questions)