Ron
Ron

Reputation: 105

Simple Python - Error in Programming

I've been stuck on this issue for over two hours, this is homework so please don't give me a direct answer just point me in the right direction.

So... This program is designed to input a "speed limit", input "Current speed" and give you a print response of either "Speed OK" (if you are below or equal to the speed limit) and "Slow down!" (if you are speeding). When I input the following data, as required on the task: Speed Limit of 50. Current Speed of 50, 45, 55, 52, and 50.

The answer should read -

Speed limit: 50
Current speed: 50
Speed OK
Current speed: 45
Speed OK
Current speed: 55
Slow down!
Current speed: 52
Slow down!
Current speed: 50
Speed OK
Current speed:(White space)

Instead I get -

Current speed: 50
Speed OK
Current speed: 45
*Then the program stops.*

My Program Reads -

limit = int(input("Speed limit: "))
speed = int(input("Current speed: "))
if speed <= limit:
  print("Speed OK")
  speed = int(input("Current speed: "))
false = speed > limit
while false:
  print("Slow down!")
  speed = int(input("Current speed: "))

If somebody could point me in the right direction that would be wonderful ;).

Cheers,

Ron

Upvotes: 4

Views: 201

Answers (4)

Alfe
Alfe

Reputation: 59416

First of all, keep in mind that the one of the most important rules of programming is to avoid code doubling. This means that it nearly never a good idea to have the same speed = int(input("Current speed: ")) line three times in your program. If there's an error in that line, fixing it will probably leave the error in the two other places untouched.

If you follow that rule, you will probably find out that you only need one loop which should terminate on (White space) (so this check should be in the condition, not false). And inside that loop you should check with if which response your program should give.

Finally, I strongly disapprove of having a variable named false. In case speed is larger than limit, your variable false will hold the value True (which is quite peculiar and surprising). Any other programmer would probably misunderstand this.

Never forget that writing a program also is communicating with the next programmer who has to maintain your code.

Upvotes: 4

Cyril Gips
Cyril Gips

Reputation: 25

You could set a counter for how often the loop returns, either with a fixed limit or with a user input limit.

The int(input("...") would sit inside the loop but outside the True/ False check.

Since there are only 2 possible answers ("Speed OK" and "Slow down") look into how to verify for this boolean.

Spoiler.

Upvotes: 0

Philippe T.
Philippe T.

Reputation: 1192

Your design is in the wrong way you have to do an infinite loop

ask speed limit 
run the forever loop (based on a true condition) :
-> ask the speed
-> print message

i hope that's will help you

Upvotes: 0

froadie
froadie

Reputation: 82993

Trying to give just a pointer -

Your loop is in the wrong place. Think carefully about what code you want to keep repeating, and what code should execute once for each loop.

Try to see what happens if your first input is OVER the speed limit :)

Upvotes: 2

Related Questions