why is my code running through my while loop, when the conditions are not met? [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • How to test multiple variables against a value?

    18 answers




When I run this code, even if enter '1' or '2' or '3', my code still runs through the while loop. How do I change my code so that it will only run through the while loop if the user DOESN'T enter '1', '2' or '3'? Thank you very much!



column=input("What column is your card in? Please enter either '1', '2' or '3':")
while column != "1" or "2" or "3":
print("That is an invalid choice. You must enter either '1' or '2' or '3'")
column=input("What column is your card in? Please enter either '1', '2' or '3':")
print(column)









share|improve this question













marked as duplicate by Martijn Pieters python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Use column not in {'1', '2', '3'}; computers are not human and English grammar is hard for computers.
    – Martijn Pieters
    Nov 10 at 22:26










  • The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True.
    – Paritosh Singh
    Nov 10 at 22:32












  • Thank you so much! My code now works @MartijnPieters
    – Atara Klein
    Nov 11 at 14:36















up vote
-1
down vote

favorite













This question already has an answer here:




  • How to test multiple variables against a value?

    18 answers




When I run this code, even if enter '1' or '2' or '3', my code still runs through the while loop. How do I change my code so that it will only run through the while loop if the user DOESN'T enter '1', '2' or '3'? Thank you very much!



column=input("What column is your card in? Please enter either '1', '2' or '3':")
while column != "1" or "2" or "3":
print("That is an invalid choice. You must enter either '1' or '2' or '3'")
column=input("What column is your card in? Please enter either '1', '2' or '3':")
print(column)









share|improve this question













marked as duplicate by Martijn Pieters python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Use column not in {'1', '2', '3'}; computers are not human and English grammar is hard for computers.
    – Martijn Pieters
    Nov 10 at 22:26










  • The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True.
    – Paritosh Singh
    Nov 10 at 22:32












  • Thank you so much! My code now works @MartijnPieters
    – Atara Klein
    Nov 11 at 14:36













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • How to test multiple variables against a value?

    18 answers




When I run this code, even if enter '1' or '2' or '3', my code still runs through the while loop. How do I change my code so that it will only run through the while loop if the user DOESN'T enter '1', '2' or '3'? Thank you very much!



column=input("What column is your card in? Please enter either '1', '2' or '3':")
while column != "1" or "2" or "3":
print("That is an invalid choice. You must enter either '1' or '2' or '3'")
column=input("What column is your card in? Please enter either '1', '2' or '3':")
print(column)









share|improve this question














This question already has an answer here:




  • How to test multiple variables against a value?

    18 answers




When I run this code, even if enter '1' or '2' or '3', my code still runs through the while loop. How do I change my code so that it will only run through the while loop if the user DOESN'T enter '1', '2' or '3'? Thank you very much!



column=input("What column is your card in? Please enter either '1', '2' or '3':")
while column != "1" or "2" or "3":
print("That is an invalid choice. You must enter either '1' or '2' or '3'")
column=input("What column is your card in? Please enter either '1', '2' or '3':")
print(column)




This question already has an answer here:




  • How to test multiple variables against a value?

    18 answers








python-3.x loops while-loop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:18









Atara Klein

84




84




marked as duplicate by Martijn Pieters python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Martijn Pieters python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Use column not in {'1', '2', '3'}; computers are not human and English grammar is hard for computers.
    – Martijn Pieters
    Nov 10 at 22:26










  • The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True.
    – Paritosh Singh
    Nov 10 at 22:32












  • Thank you so much! My code now works @MartijnPieters
    – Atara Klein
    Nov 11 at 14:36














  • 1




    Use column not in {'1', '2', '3'}; computers are not human and English grammar is hard for computers.
    – Martijn Pieters
    Nov 10 at 22:26










  • The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True.
    – Paritosh Singh
    Nov 10 at 22:32












  • Thank you so much! My code now works @MartijnPieters
    – Atara Klein
    Nov 11 at 14:36








1




1




Use column not in {'1', '2', '3'}; computers are not human and English grammar is hard for computers.
– Martijn Pieters
Nov 10 at 22:26




Use column not in {'1', '2', '3'}; computers are not human and English grammar is hard for computers.
– Martijn Pieters
Nov 10 at 22:26












The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True.
– Paritosh Singh
Nov 10 at 22:32






The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True.
– Paritosh Singh
Nov 10 at 22:32














Thank you so much! My code now works @MartijnPieters
– Atara Klein
Nov 11 at 14:36




Thank you so much! My code now works @MartijnPieters
– Atara Klein
Nov 11 at 14:36












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Your condition should be for each check



while (column != "1") or (column != "2") or (column != "3"):


Without this check you say that it is non zero and hence True, the condition is satisfied for the while loop. Hence the conditions are met. 2 or 3 is not compared with column.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Your condition should be for each check



    while (column != "1") or (column != "2") or (column != "3"):


    Without this check you say that it is non zero and hence True, the condition is satisfied for the while loop. Hence the conditions are met. 2 or 3 is not compared with column.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Your condition should be for each check



      while (column != "1") or (column != "2") or (column != "3"):


      Without this check you say that it is non zero and hence True, the condition is satisfied for the while loop. Hence the conditions are met. 2 or 3 is not compared with column.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Your condition should be for each check



        while (column != "1") or (column != "2") or (column != "3"):


        Without this check you say that it is non zero and hence True, the condition is satisfied for the while loop. Hence the conditions are met. 2 or 3 is not compared with column.






        share|improve this answer












        Your condition should be for each check



        while (column != "1") or (column != "2") or (column != "3"):


        Without this check you say that it is non zero and hence True, the condition is satisfied for the while loop. Hence the conditions are met. 2 or 3 is not compared with column.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 22:22









        Ashok KS

        203214




        203214















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma