Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python program

i want to Design a program that calculates and displays the number of miles per hour over the speed limit that a speeding driver was doing. The program should ask for the speed limit and the driver’s speed. Validate the input as follows:  The speed limit should be at least 20, but not greater than 70.  The driver’s speed should be at least the value entered for the speed limit (otherwise the driver was not speeding).

I fell there is something wrong with my code, can someone fix it for me, and tell me what was the wrong things This is my Code:-

Declare Integer speedLimit, speed, minSpeed, maxSpeed, overSpeed

Module Module1

def main(): speedLimit = int(input("Please enter speed limit, if it's between 19 and 71 mph, then press enter: ")) print("You entered: ", speedLimit)

while(speedLimit < 20 and speedLimit < 70):
    if (speedLimit < 20):
        print("Sorry, speed limit less than 20 mph. Try again ")
    elif(speedLimit > 70):
        print("Sorry, speed limit more than 70 mph.Try again.")
    else:
        ("ddjid")


driverSpeed = int(input("How fast are you driving: "))

if (driverSpeed > speedLimit):
    print("Alert! You are driving over the speed limit")
else:
    print("You are driving at or under the speed limit.")

main()

2 Answers

Hello Hanan,

first of all your code should be calling main() in that way:

if __name__ == "__main__":
    main()

Second you should move your input statement in the while loop like this:

def main():
   speedLimit = 0
   while(speedLimit < 20 or speedLimit > 70):
       speedLimit = \
           int(input("Please enter speed limit, if it's between 19 and 71 mph, then press enter: "))
       print("You entered: ", speedLimit)
       if (speedLimit < 20):
            print("Sorry, speed limit less than 20 mph. Try again ")
       elif(speedLimit > 70):
            print("Sorry, speed limit more than 70 mph.Try again.")

Third your while statement was not correct: I corrected it in the above version of the code. The rest of your code should work fine if you make these corrections. I hope that helps.

Hello Sabine :)

Thank you so much for your Help, it worked perfect