Note that there are some explanatory texts on larger screens.

plurals
  1. POKeyError when iterating on lists
    primarykey
    data
    text
    <p>I'm sorry if this is a stupid question but I've been thinking about it a lot and looked through Jinja docs but so far to no avail. Short background: I've just completed Udacity's CS101 and CS253 and am about to help a friend build a booking system for a yoga studio. It's all on Google Appengine, just like </p> <p>I want to have a list of available yoga classes similar to this:</p> <pre><code>Monday Dynamic Yoga with Mary 6pm Soft yoga with Susie 8pm Wednesday Hatha yoga with Bob 5pm Hot yoga with Alice 7pm </code></pre> <p>So I want to fetch the list of classes, then see if there is a yoga class on Monday. If there is one I add 'Monday' to the list and all the Monday classes, and so on with all the other days. Like this: </p> <p><code>day_output1 = ['Monday',['Dynamic Yoga with Mary 6pm'],['Soft yoga with Su..']]</code></p> <pre><code>day_output2 = ['Wednesday',['Hatha yoga with Bob 5pm'],['Hot yoga with Al...']] </code></pre> <p>And then add these to a list for the whole week, which is then sent to the template:</p> <pre><code>weekly_classes = [day_output1, day_output2] </code></pre> <p>Right now I'm getting a KeyError, which means it doesnt find the Key, but I dont understand why?</p> <pre><code> File "/Users/username/YogaSchemat/yogaschema/main.py", line 113, in get day = d[n] KeyError: 1 </code></pre> <p>With this code... Thanks in advance guys!</p> <pre><code>d = { "1": 'Monday', "2": 'Tuesday', "3": 'Wednesday', "4": 'Thursday', "5": 'Friday', "6": 'Saturday', "7": 'Sunday' } def get_classes(): yoga_classes = Schema.all() #appengine DB request if yoga_classes: weekly_classes = [] #list that will be sent to template for n in range(1,8): for e in yoga_classes: if e.weekday == n: day = d[n] #getting weekday from d class_details = [] # class_details.append(e) day_output = [day,class_details] weekly_classes.append(day_output) self.response.out.write(weekly_classes) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload