So my semester is almost over and I was dumb when I signed up for intro to programming distance learning, I thought I would understand it, but now I guess I know I can't. Anyways here is the problem. A speeding ticket fine policy in Podunksville is $50 plus $5 for each MPH over the limit plus a penalty of $200 for any speed over 90mph. The first function should accept the speed limit, the 2nd function should accept the clocked speed, and the third function should determine and print the fine, if there is one, or print a message indicating that hte speed was legal. Here is what I have so far. I don't know why I keep getting an error in the last line.

def ask_limit():
          limit = float(raw_input ("What was the speed limit? "))
          speed = float(input ("What was your clocked speed? "))
          return limit
def ask_speed():
          speed = float(input ("What was your clocked speed? "))
          return speed
        
def findfine(limit, speed):
          if speed > 90:
                 bigfine = ((speed - limit) * 5 + 250)
                 print "your fine is", bigfine
          elif speed < limit:
                 print "you were traveling a legal speed"
                 
          else:
                 fine = ((speed - limit * 5) + 50)
                 print "your fine is", fine
          
          return fine, bigfine

def main(fine, bigfine):
        limit = ask_limit()
        speed = ask_speed()
        fine, bigfine = findfine(limit,speed)
main()

I got it figured out

def ask_limit():
    limit = float(input ("What was the speed limit? "))
    
    return limit
def ask_speed():
    speed = float(input ("What was your clocked speed? "))
    return speed
def findfine(speed, limit):
    if speed > 90:
        bigfine = ((speed - limit) * 5 + 250)
        print "your fine is", bigfine
    elif speed <= limit:
        print "you were traveling a legal speed"
    else:
        fine = ((speed - limit) * 5 + 50)
        print "your fine is", fine
        
def main():
    limit = ask_limit()
    speed = ask_speed()
    findfine(speed, limit)

main()

But I'm trying to make it so that the limit can not be less than 10 or more than 90, and in the 2nd function it has to validate the clocked speed, it can not be less than 5 or more than 130. I must loops to do the validation.

Is this right? I don't know how to make is so that once it asks for the limit again to set the parameters so it won't accept a number that is less than 10 or more than 80

def ask_limit():
    limit = float(input ("What was the speed limit? "))
    while limit < 10:
        print "speed limit must be faster than 10mph"
        limit = float(input ("What was the speed limit? "))
    while limit > 80:
        print "speed limit much be less than 80mph"
        limit = float(input ("What was the speed limit? "))
    
    return limit
def ask_speed():
    speed = float(input ("What was your clocked speed? "))
    while speed < 5:
        print "Clocked speed must be faster than 5mph"
        speed = float(input ("What was your clocked speed? "))
    while speed > 130:
        print "You know your radar gun doesn't go that high"
        speed = float(input ("What was your clocked speed? "))
    return speed
def findfine(speed, limit):
    if speed > 90:
        bigfine = ((speed - limit) * 5 + 250)
        print "your fine is", bigfine
    elif speed <= limit:
        print "you were traveling a legal speed"
    else:
        fine = ((speed - limit) * 5 + 50)
        print "your fine is", fine
        
def main():
    limit = ask_limit()
    speed = ask_speed()
    findfine(speed, limit)

main()

Note that if someone enters anything other than a number, the program will fail, but It doesn't say anything about testing the input so you are probably OK as is. If you have 2 while() loops in a row, then you will have to enter the data twice, once for each loop.

def ask_speed():
    speed = 0
    while (speed < 5) or (speed > 130):
        print "\nClocked speed must be faster than 5mph and less than 130 mph"
        speed = float(input ("What was your clocked speed? "))
        if speed < 5.0:
             print "Clocked speed must be faster than 5mph"
        elif speed > 130:
             print "You know your radar gun doesn't go that high"
    return speed
def ask_speed():
    speed = 0
    while (speed < 5) or (speed > 130):
        print "\nClocked speed must be faster than 5mph and less than 130 mph"
        speed = float(input ("What was your clocked speed? "))
        if speed < 5.0:
             print "Clocked speed must be faster than 5mph"
        elif speed > 130:
             print "You know your radar gun doesn't go that high"
    return speed

I would remove the if statement in that loop, otherwise you print information twice...

I would remove the if statement in that loop, otherwise you print information twice...

It prints separate statements. One if the speed is less than 5 and another if it is greater than 130. The if/elif = (if) (else+if) so at most only one executes. They are there so that nothing is printed for speeds between 5 and 130.

I tested it:

jordan@ubuntu-gateway:~$ python tmp.py 

Clocked speed must be faster than 5mph and less than 130 mph
What was your clocked speed? 3
Clocked speed must be faster than 5mph

Clocked speed must be faster than 5mph and less than 130 mph
What was your clocked speed? 5
jordan@ubuntu-gateway:~$

note that it uses the words "Clocked speed must be faster than 5mph" twice. That's why I say it's redundant, and why the if: elif: pair should be taken out...

Clocked speed must be faster than 5mph and less than 130 mph
What was your clocked speed? 3
Clocked speed must be faster than 5mph

Clocked speed must be faster than 5mph and less than 130 mph
What was your clocked speed? 5

This is indeed how it is supposed to work. The first time the speed was 3, so it prints "Clocked speed must be faster than 5mph", and asks again since the speed wasn't between 5-130. It is the while loop that is doing this, not the if statements. If you enter a speed between 5 & 130 the first time, no message will be printed. It will however print messages as many times as you enter a speed less than 5 or greater than 130.

Assume the limit is 100 km/h.
The rules for fines are:
 $30 for speeds less than 10 km/h over the limit.
 $80 for 10-15 km/h over
 $120 for 15-20 km/h over
 $170 for 20-25 km/h over

Can someone help me with this please?

if 0 < (speed - limit) < 10:
    fine=$30
elif etc.

I tried both solution but it does not work on my python v.3.4

Please post the code that you tried and why it fails.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.