I need to write this code but I can't figure out how to get it to count the mphs over the speed limit the person goes.
speed = int(input("How fast where they going? (in mph) "))
limit = int(input("What is the speed limit? "))
if speed > limit:
print("Illegal Speed!")
if speed > 90:
fine = 250
for i in speed:
fine = fine + 5
print("Their fine is $", fine)
else:
fine = 50
for i in speed:
fine = fine + 5
print("Their fine is $", fine)
if speed <= limit:
print("Legal Speed")
Considering that what Charles commented about your intention of adding 5 for every mile per hour over the speed limit is correct, I believe this solution would be cleaner:
speed = int(input("How fast where they going? (in mph) "))
limit = int(input("What is the speed limit? "))
if speed > limit:
print("Illegal Speed!")
if speed > 90:
fine = 250
else:
fine = 50
fine += (speed - limit) * 5
print("Their fine is $", fine)
else:
print("Legal Speed")
Since the fine calculation and print are the same for both cases, it's easier to put it in a "common zone" and use the if/else just to set "fine" initial value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With