Speeding Ticket Fine Calulation
Question:
This program calculates the speeding ticket costs
The policy is that every ticket is $50 plus $5 for each mph over the limit, plus a penalty of $200 for any speed over 90 mph. However, if a speed is not 5 mph over the speed limit, that ticket can be waived. (e.g. if a speed limit is 37, you may drive at 41 but not at 42.) The program asks the user how many tickets they have, and for each ticket asks for the speed limit & clocked speed. I want to calculate and print the fine for each ticket and return the sum total of all the fines and Report a fine of $0 for a ticket if the clocked speed was not over the limit or if the ticket can be waived.
This is my code which works fine for single ticket i want help for doing this for many tickets i am new to python.
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()
Answers:
Whenever you want to repeat something, consider using either a while
or for
loop. A while
loop is for when you want to continuously repeat until some condition is True
, and a for
loop is for when you want to do something a set number of times. For example:
def main():
done = False
while not done:
limit = ask_limit()
speed = ask_speed()
findfine(speed, limit)
done = raw_input("Done? ") == "yes"
Notice I used another raw_input
to make sure the user can stop the program.
Keep in mind that the limit and the speed is not remembered each time. As the program currently stands, it cannot return the sum total of the tickets. I’ll leave it as an exercise to you to figure out the best way of doing so.
This program calculates the speeding ticket costs
The policy is that every ticket is $50 plus $5 for each mph over the limit, plus a penalty of $200 for any speed over 90 mph. However, if a speed is not 5 mph over the speed limit, that ticket can be waived. (e.g. if a speed limit is 37, you may drive at 41 but not at 42.) The program asks the user how many tickets they have, and for each ticket asks for the speed limit & clocked speed. I want to calculate and print the fine for each ticket and return the sum total of all the fines and Report a fine of $0 for a ticket if the clocked speed was not over the limit or if the ticket can be waived.
This is my code which works fine for single ticket i want help for doing this for many tickets i am new to python.
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()
Whenever you want to repeat something, consider using either a while
or for
loop. A while
loop is for when you want to continuously repeat until some condition is True
, and a for
loop is for when you want to do something a set number of times. For example:
def main():
done = False
while not done:
limit = ask_limit()
speed = ask_speed()
findfine(speed, limit)
done = raw_input("Done? ") == "yes"
Notice I used another raw_input
to make sure the user can stop the program.
Keep in mind that the limit and the speed is not remembered each time. As the program currently stands, it cannot return the sum total of the tickets. I’ll leave it as an exercise to you to figure out the best way of doing so.