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 trialUnsubscribed User
7,260 PointsCheck student points before update
Hi all!
As mentioned in this video lesson, when creating the try & except and getting the student record, I am trying to add an if statement to check if the points have changed. If they remained unchanged then nothing should happen.
I've been trying to give it a go with an if statement as per below, but I am getting an invalid syntax error at the line: if student_record.points == student['points'].
I'm sure the answer is pretty straightforward but I am not seeing the light at this point. :D
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'])
if student_record.points == student['points']
pass
else:
student_record.points = student['points']
student_record.save()
I've also tried it the other way around:
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'])
if student_record.points not == student['points']
student_record.points = student['points']
student_record.save()
else:
pass
1 Answer
Unsubscribed User
7,260 PointsHi Nathan,
Many thanks for your swift reply. You're suggestion of using != actually seemed to have worked. I wonder however why the == was giving a syntax error.
I'm only trying to implement what Kenneth implied would be a good thing to try to do, instead of just overwriting the same value. My code should be identical as in the video except where I try to inplement an IF statement to check if the value is identical or not. For reference, I included the code in full here:
from peewee import *
import sqlite3
db = SqliteDatabase('students.db')
class Student(Model):
username = CharField(max_length=255, unique=True)
points = IntegerField(default=0)
class Meta:
database = db
students = [
{'username': 'kevinjacobs',
'points': 4488},
{'username': 'chalkers',
'points': 11561},
{'username': 'joykesten2',
'points': 7363},
{'username': 'craigsdennis',
'points': 4079},
{'username': 'davemcfarland',
'points': 45468},
]
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'])
if student_record.points != student['points']:
student_record.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 is {0.username}!".format(top_student()))
Nathan Tallack
22,160 PointsSo running through that code you get the output that davemcfarland is the top student when you run it, which is correct.
Does this not run for you? If not, can you paste the output of the script when you run it. Becuase in my workspace it runs perfectly. :)
Nathan Tallack
22,160 PointsNathan Tallack
22,160 PointsWithout having a link to your workspace to see all of your code I cannot trouble shoot for you. However, a few things at first glance.
1: Try using != as your "not equal to" operator. 2: Why are you wanting to check if hte points match? Why not just set them anyway. It is ok to overwrite with same value. 3: No need to do a pass on else. Just don't declare the else. 4: Why are you calling the Student.get type method rather than calling student_record.get object method on your Student object directly?
If you can share your workspace I could help troubleshoot. If you are not using workspaces, upload your code to github or something similar so that we can see all of your code and help you troubleshoot. :)
Keep up the great work! And be sure to embrace peewee. It is the win! :D