Bummer! You must be logged in to access this page.

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! While you're at it, check out some resources Treehouse students have shared here.

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

Databases - top student

I got Kenneth's code working in workspaces, until it can time to change his number so that he became the winner. Then I got pages of errors, and I cannot figure out why, nor can I get it back to where it is running again. Here is the code:

from peewee import *

db = SqliteDatabase('students.db')


class Student(Model):
  username = CharField(max_length=255, unique=True)
  points = IntegerField(default=0)

  class Meta:
    database = db

students = [
  {'username': 'kennethlove',
   'points': 14889},
  {'username': 'chalkers',
   'points': 11912},
  {'username': 'joykesten2',
   'points': 7363},
  {'username': 'craigsdennis',
   'points': 4079},
  {'username': 'davemcfarland',
   'points': 13717},
]

def add_students():
  for student in students:
    try:
      Student.create(username=student['username'],
                   points=student['points'])
    except IntegrityError:
      student_record = Student.get(username=student['username'])
      student_recpord.points = student['points']
      student_record.save()

def top_student():
  student = Student.select().order_by(Student.points.desc()).get()
  return student

if __name__ == '__main__':
  db.connect()
  db.create_tables([Student], safe = True)
  add_students()
  print("Our top student right now is: {0.username}.".format(top_student()))

2 Answers

You have a typo in your add_students function (student_recpord instead of student_record).

Also, when submitting code, be sure to put a blank line above and below the code markdown tags, otherwise the code won't render properly . Good luck!

I had no idea about the blank lines, thanks! I just found the typo by printing out the code and looking at it, moments before I saw that you had responded. Sorry to put you through that!

No worries! Sometimes looking away from the problem for a moment is all you need.