Python – How to calculate numbers between variables

How to calculate numbers between variables… here is a solution to the problem.

How to calculate numbers between variables

I

need to write this code, but I don’t know how to get it to calculate miles per hour that exceeds the speed limit for the person to travel.

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")

Solution

Considering that Charles commented that it is correct that you intend to add 5 per mile per hour when you exceed the speed limit, I believe this solution will be more concise:

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 fine-grained calculations and printing are the same in both cases, it is easier to place it in the “public” area and use if/else to set the “fine” initial value.

Related Problems and Solutions