logical operator not working in while statement in python [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
How to test multiple variables against a value?
18 answers
deMorgan rules explained
8 answers
Python 'if' and 'while' conditions not working
1 answer
my code:
pokemon_choice = input("Choose a pokemon: ")
while pokemon_choice != ("squirtle") or pokemon_choice != ("bulbasaur"):
pokemon_choice = input("invalid choice, choose a pokemon: ")
I'm new to python so this may be a stupid question but, if the user input for the first pokemon_choice variable were to be bulbasaur or squirtle it would not run the while loop... right? The thing is, when I typed bulbasaur, and only bulbasaur, even though i typed bulbasaur exactly as it should be it still ran the while loop. When I type squirtle however it seems to work just fine, ignoring the while loop. Why is it still running the loop???
python
marked as duplicate by Jean-François Fabre
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 19 at 8:33
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.
|
show 3 more comments
up vote
-1
down vote
favorite
This question already has an answer here:
How to test multiple variables against a value?
18 answers
deMorgan rules explained
8 answers
Python 'if' and 'while' conditions not working
1 answer
my code:
pokemon_choice = input("Choose a pokemon: ")
while pokemon_choice != ("squirtle") or pokemon_choice != ("bulbasaur"):
pokemon_choice = input("invalid choice, choose a pokemon: ")
I'm new to python so this may be a stupid question but, if the user input for the first pokemon_choice variable were to be bulbasaur or squirtle it would not run the while loop... right? The thing is, when I typed bulbasaur, and only bulbasaur, even though i typed bulbasaur exactly as it should be it still ran the while loop. When I type squirtle however it seems to work just fine, ignoring the while loop. Why is it still running the loop???
python
marked as duplicate by Jean-François Fabre
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 19 at 8:33
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.
3
while pokemon_choice != ("squirtle") and pokemon_choice != ("bulbasaur"):
– Jean-François Fabre
Nov 19 at 8:33
1
you should useandin place ofor. So, your loop will run whenever the choice is not bulbasaur and not squirtle!
– rv7
Nov 19 at 8:35
1
@Jean-FrançoisFabre that's the wrong dupe, imo.
– timgeb
Nov 19 at 8:36
1
@Jean-FrançoisFabre: that duplicate doesn't explain about De Morgan's boolean laws, which is the real problem here.
– Martijn Pieters♦
Nov 19 at 8:38
1
Your boolean test is true for all possible cases. Whenpokemon_choiceis set to"squirtle"thenpokemon_choice != ("bulbasaur")is true, and because you usedor, that makes the whole test true. Whenpokemon_choiceis set to"bulbasaur", thenpokemon_choice != ("squirtle")is true, and because you usedor, the whole test is true. Useandto ensure both tests are taken into account.
– Martijn Pieters♦
Nov 19 at 8:41
|
show 3 more comments
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
deMorgan rules explained
8 answers
Python 'if' and 'while' conditions not working
1 answer
my code:
pokemon_choice = input("Choose a pokemon: ")
while pokemon_choice != ("squirtle") or pokemon_choice != ("bulbasaur"):
pokemon_choice = input("invalid choice, choose a pokemon: ")
I'm new to python so this may be a stupid question but, if the user input for the first pokemon_choice variable were to be bulbasaur or squirtle it would not run the while loop... right? The thing is, when I typed bulbasaur, and only bulbasaur, even though i typed bulbasaur exactly as it should be it still ran the while loop. When I type squirtle however it seems to work just fine, ignoring the while loop. Why is it still running the loop???
python
This question already has an answer here:
How to test multiple variables against a value?
18 answers
deMorgan rules explained
8 answers
Python 'if' and 'while' conditions not working
1 answer
my code:
pokemon_choice = input("Choose a pokemon: ")
while pokemon_choice != ("squirtle") or pokemon_choice != ("bulbasaur"):
pokemon_choice = input("invalid choice, choose a pokemon: ")
I'm new to python so this may be a stupid question but, if the user input for the first pokemon_choice variable were to be bulbasaur or squirtle it would not run the while loop... right? The thing is, when I typed bulbasaur, and only bulbasaur, even though i typed bulbasaur exactly as it should be it still ran the while loop. When I type squirtle however it seems to work just fine, ignoring the while loop. Why is it still running the loop???
This question already has an answer here:
How to test multiple variables against a value?
18 answers
deMorgan rules explained
8 answers
Python 'if' and 'while' conditions not working
1 answer
python
python
asked Nov 19 at 8:32
NefariousGent
1
1
marked as duplicate by Jean-François Fabre
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 19 at 8:33
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 Jean-François Fabre
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 19 at 8:33
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.
3
while pokemon_choice != ("squirtle") and pokemon_choice != ("bulbasaur"):
– Jean-François Fabre
Nov 19 at 8:33
1
you should useandin place ofor. So, your loop will run whenever the choice is not bulbasaur and not squirtle!
– rv7
Nov 19 at 8:35
1
@Jean-FrançoisFabre that's the wrong dupe, imo.
– timgeb
Nov 19 at 8:36
1
@Jean-FrançoisFabre: that duplicate doesn't explain about De Morgan's boolean laws, which is the real problem here.
– Martijn Pieters♦
Nov 19 at 8:38
1
Your boolean test is true for all possible cases. Whenpokemon_choiceis set to"squirtle"thenpokemon_choice != ("bulbasaur")is true, and because you usedor, that makes the whole test true. Whenpokemon_choiceis set to"bulbasaur", thenpokemon_choice != ("squirtle")is true, and because you usedor, the whole test is true. Useandto ensure both tests are taken into account.
– Martijn Pieters♦
Nov 19 at 8:41
|
show 3 more comments
3
while pokemon_choice != ("squirtle") and pokemon_choice != ("bulbasaur"):
– Jean-François Fabre
Nov 19 at 8:33
1
you should useandin place ofor. So, your loop will run whenever the choice is not bulbasaur and not squirtle!
– rv7
Nov 19 at 8:35
1
@Jean-FrançoisFabre that's the wrong dupe, imo.
– timgeb
Nov 19 at 8:36
1
@Jean-FrançoisFabre: that duplicate doesn't explain about De Morgan's boolean laws, which is the real problem here.
– Martijn Pieters♦
Nov 19 at 8:38
1
Your boolean test is true for all possible cases. Whenpokemon_choiceis set to"squirtle"thenpokemon_choice != ("bulbasaur")is true, and because you usedor, that makes the whole test true. Whenpokemon_choiceis set to"bulbasaur", thenpokemon_choice != ("squirtle")is true, and because you usedor, the whole test is true. Useandto ensure both tests are taken into account.
– Martijn Pieters♦
Nov 19 at 8:41
3
3
while pokemon_choice != ("squirtle") and pokemon_choice != ("bulbasaur"):– Jean-François Fabre
Nov 19 at 8:33
while pokemon_choice != ("squirtle") and pokemon_choice != ("bulbasaur"):– Jean-François Fabre
Nov 19 at 8:33
1
1
you should use
and in place of or. So, your loop will run whenever the choice is not bulbasaur and not squirtle!– rv7
Nov 19 at 8:35
you should use
and in place of or. So, your loop will run whenever the choice is not bulbasaur and not squirtle!– rv7
Nov 19 at 8:35
1
1
@Jean-FrançoisFabre that's the wrong dupe, imo.
– timgeb
Nov 19 at 8:36
@Jean-FrançoisFabre that's the wrong dupe, imo.
– timgeb
Nov 19 at 8:36
1
1
@Jean-FrançoisFabre: that duplicate doesn't explain about De Morgan's boolean laws, which is the real problem here.
– Martijn Pieters♦
Nov 19 at 8:38
@Jean-FrançoisFabre: that duplicate doesn't explain about De Morgan's boolean laws, which is the real problem here.
– Martijn Pieters♦
Nov 19 at 8:38
1
1
Your boolean test is true for all possible cases. When
pokemon_choice is set to "squirtle" then pokemon_choice != ("bulbasaur") is true, and because you used or, that makes the whole test true. When pokemon_choice is set to "bulbasaur", then pokemon_choice != ("squirtle") is true, and because you used or, the whole test is true. Use and to ensure both tests are taken into account.– Martijn Pieters♦
Nov 19 at 8:41
Your boolean test is true for all possible cases. When
pokemon_choice is set to "squirtle" then pokemon_choice != ("bulbasaur") is true, and because you used or, that makes the whole test true. When pokemon_choice is set to "bulbasaur", then pokemon_choice != ("squirtle") is true, and because you used or, the whole test is true. Use and to ensure both tests are taken into account.– Martijn Pieters♦
Nov 19 at 8:41
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
3
while pokemon_choice != ("squirtle") and pokemon_choice != ("bulbasaur"):– Jean-François Fabre
Nov 19 at 8:33
1
you should use
andin place ofor. So, your loop will run whenever the choice is not bulbasaur and not squirtle!– rv7
Nov 19 at 8:35
1
@Jean-FrançoisFabre that's the wrong dupe, imo.
– timgeb
Nov 19 at 8:36
1
@Jean-FrançoisFabre: that duplicate doesn't explain about De Morgan's boolean laws, which is the real problem here.
– Martijn Pieters♦
Nov 19 at 8:38
1
Your boolean test is true for all possible cases. When
pokemon_choiceis set to"squirtle"thenpokemon_choice != ("bulbasaur")is true, and because you usedor, that makes the whole test true. Whenpokemon_choiceis set to"bulbasaur", thenpokemon_choice != ("squirtle")is true, and because you usedor, the whole test is true. Useandto ensure both tests are taken into account.– Martijn Pieters♦
Nov 19 at 8:41