0

I am using a while loop inside a if / else condition. For some reason under one condition the while loop does not work. The condition is shown in my code below. Under these conditions I would assume that the else condition should be utilized and the weight and the max_speed should be decreased until both while conditions are not valid anymore. What am I doing wrong?

weight = 0
max_speed = 15

if weight == 0 and max_speed <= 10:
    while weight == 0 and max_speed <= 10:
        weight=weight+1
        print(weight)
        print(max_speed)
else:
    while weight != 0 and max_speed > 10:
        weight = weight-1
        max_speed=max_speed-1
        print(weight)
        print(max_speed)
5
  • What is the use of while loop here? I can't just understand the logic behind it. What are you trying to achieve actually? Commented Sep 6, 2016 at 6:52
  • 5
    It goes to the else branch but since weight is 0 the weight != 0 evaluates to False and therefore the whole weight != 0 and max_speed > 10 expression evaluates to False. And that is why the while loop doesn't run. Commented Sep 6, 2016 at 6:56
  • Do you want the weight=0 and max_speed=10 ?? Commented Sep 6, 2016 at 6:56
  • It's a simplified example of my real problem (for which the code is more longer). I have two conditions, which in this example are weight and max_speed. If my weight and max speed are low values then I would like to increase the weight just a little. And if the weight and max_speed (or one of them) is a high value I would like to decrease the values until they are in my desired range. Commented Sep 6, 2016 at 6:58
  • You have 2 variables but only 2 cases/combinations covered with the if and else conditions. Whatever you want to achieve, you should use more else-statements or use or instead of and to cover all combination of the 2 variables weight and max_speed. Commented Sep 6, 2016 at 7:02

2 Answers 2

1

Assuming that you need your weight=0 and max_speed=10; You can do this ->

weight = 0
max_speed = 15

while weight !=0 or max_speed > 10:
    if weight>0: 
        weight = weight-1
    else:  
        weight = weight+1
    if max_speed>10:
        max_speed=max_speed-1
    print("{} {}".format(weight, max_speed))

Your output looks like ->

1 14
0 13
1 12
0 11
1 10
0 10
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are confused between or and and.

and means that the expression will be True if both condition satisfies. Where or means any condition satisfies.

Now based on your code:

weight = 0
max_speed = 15

if weight == 0 and max_speed <= 10:
    # Goes to else block since max_speed = 15 which is >10
else:
    # This while won't be executed since weight = 0
    while weight != 0 and max_speed > 10:

5 Comments

If I use or, the code keeps running and does not end.
It is based on the logic what you want to achieve. I have updated the explanation in my answer, may be that will help.
In the first while loop , you should add max_speed += 1 for change max_speed value
what is the need of two different while loop here? To me @jbsu32's answer is more appropriate
@AhsanulHaque: I was not telling him the solution, but the cause of the problem. And left the implementation to the user to implement which I believe will be more helpful to him.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.