Looking for revisions of previously written code
<p>I've noticed that lately my assignment grades have been getting lower as the class has advanced in Python, and I was wondering if anyone could help me see what I did wrong with these code snippets and why they were supposedly wrong. This may be a long post, but any help that'll stop me from making these mistakes in the future is appreciated.</p>

<pre><code>def geometric(l):
    'list(int) ==&gt; bool, returns True if the integers form a geometric sequence'
    res = False
    if (l[1] / l[0]) == (l[2] / l[1]):
        res = True
    return res
</code></pre>

<p>This is a code that was considered wrong. As the code says, it takes a list of ints and returns whether or not they're in a geometric sequence. My book says a sequence is geometric if a1/a0 equals a2/a1, which worked just fine for 2, 4, 8, 16, etc. I just realized that the issue with this one is that it only checks the first two indices and ignores the rest of the numbers. What could I write that would fix this?</p>

<pre><code>def letter2number(grade):
    '''string ==&gt; int, returns the numeric equivalent to the letter grade, adding 0.3 if the grade contains a +
    and subtracts 0.3 if the grade contains a -'''
    res = 0
    if 'F' in grade:
        res = 0
    elif 'D' in grade:
        res = 1
    elif 'C' in grade:
        res = 2
    elif 'B' in grade:
        res = 3
    elif 'A' in grade:
        res = 4
    if '+' in grade:
        res += 0.3
    elif '-' in grade:
        res -= 0.3
    return res
</code></pre>

<p>This wasn't considered wrong, exactly, but it was much longer than he intended I guess. A comment he wrote on the file was "# set res to index of grade in grades," but since it was an independent lab I couldn't ask for help. I attempted to give each grade its value in a list but I couldn't index it correctly.</p>

<pre><code>def leap(n):
    'int ==&gt; bool, returns True if the year is a leap year, False otherwise'
    res = False
    if n % 4 == 0 and n % 400 == 0 or not n % 100 == 0:
        res = True
    return res
</code></pre>

<p>Figuring out an if statement that translated into "A year is a leap year if it is divisible by 4 but not by 100, unless it is divisible by 400" was confusing for me. Apparently what I wrote works for all odd numbers, but I can't really figure out why.</p>
