Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count numbers inbetween variables

Tags:

python

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")
like image 617
Elijah Avatar asked Mar 25 '26 15:03

Elijah


1 Answers

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.

like image 168
Pedro Martins de Souza Avatar answered Mar 28 '26 04:03

Pedro Martins de Souza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!