0

I have a while block, and inside it I have decision making blocks including elif blocks. I want that the program exits from the while block when obje_error == 0, but the while block repeats instead.

I suppose that obje_error == 0 when the other conditions are not complete. I removed the elif blocks and it worked, but I want that the all blocks go to work.

You can see the code below:

while (obje_error == 1):    
    Sen=raw_input("tas mi,kagit mi yoksa makas mi?")
    if (Sen != "tas") :
        print "Lutfen tas,kagit ya da makas'i secin..."
        obje_error=1
    elif (Sen != "kagit"):
        print "Lutfen tas,kagit ya da makas'i secin..."
        obje_error=1
    elif Sen != "makas":
        print "Lutfen tas,kagit ya da makas'i secin..."
        obje_error=1
    else:
        obje_error=0

Can you tell me what the problem is and what I can do?

3
  • 4
    Sen can only hold one string, so it'll be tas or kagit or makas or something else, so it'll always trigger either the first or the second of the three != tests. It'll never get to the last else statement. Did you perhaps mean to test for == instead? Commented Nov 8, 2012 at 21:57
  • What @MartijnPieters said. I'm thinking you may want in tests, or a single if with multiple conditions ored together. Also, if all you want is an infinite loop to break out of when a condition is satisfied, for with break tends to be cleaner. Could you lay out your requirements for this whole chunk of code, from a higher level? Commented Nov 8, 2012 at 21:59
  • Thank you for your answer.I resolved the problem using tuples like this:if Sen in ("tas","kagit","makas") :obje_error=0 Commented Nov 9, 2012 at 18:53

1 Answer 1

1

If the string is equal to one of the strings you're testing against, it will be unequal to all the others. Thus one of the if statements is guaranteed to succeed, and you'll never reach the else at the end.

You probably wanted == instead of !=.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer but I want that when the user-input is unequal to "tas","kagit" or "makas" the while loop must be repeat. Wherefore I write if (Sen != "tas") but when I give the values "tas","kagit" or "makas" from the terminal it's repeating too like other input that are unequal to "tas","kagit" or "makas" and doesn't exit from the while loop.
@ErenCan, as long as you set obje_error=1 then the while loop will repeat.

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.