Can someone check this code, I'm new to for loops


The code prints the wrong value.

If I were to type in 60 then 65, it should say I have gotten 10 demerits

speedLimit = int(input("What is the speed limit in this area? "))
speed = int(input("What speed was the vehicle traveling at? "))
speedOverLimit = int(speed-speedLimit)


for speedOverLimit in range(1,11):
for speedOerLimit in range(12,21):
for speedOverLimit in range(22,31):
for speedOverLimit in range(32,36):
for speedOverLimit in range(36,1000):



Please post a link to the exercise. This does not look familiar.

The problem suggests more of a control flow solution than a loop construct:

sol = speedOverLimit    # for simplicity
if sol in range(1, 11):
    demerits = 10
elif sol in range(11, 21):
    demerits = 20
elif sol in range(21, 31)
    demerits = 30
elif sol in range(31, 41):
    demerits = 40
    demerits = 50

I've arbitrarily tweaked the numbers to continue the pattern.

Consider a mathematical approach to simplify the above:

    if sol > 40:
        demerits = 50
        coef = sol / 10 + 1
        demerits = coef * 10

Given fixed data ranges, this is the most efficient approach.

We can emulate a switch using a dictionary. Take for example this method of assigning a letter grade:

def get_letter_grade(grade):
    G = {'F':(0,60),'D':(60,70),'C':(70,80),'B':(80,90),'A':(90,101)}
    for letter in G:
        a, b = G[letter]
        if ( a <= grade and grade < b ):
            return letter