Skip to content Skip to sidebar Skip to footer

Multiple Conditions With While Loop In Python

I am having problems including multiple statements with while loop in python. It works perfectly fine with single condition but when i include multiple conditions, the loop does no

Solution 1:

You need to use and; you want the loop to continue if all conditions are met, not just one:

while (name != ".") and (name != "!") and (name != "?"):

You don't need the parentheses however.

Better would be to test for membership here:

while name not in'.!?':

Solution 2:

This condition:

(name != ".") or (name != "!") or (name != "?")

is always true. It could only be false of all three subconditions were false, which would require that name were equal to "." and "!" and "?" simultaneously.

You mean:

while (name != ".") and (name != "!") and (name != "?"):

or, more simply,

while name not in { '.', '!', '?' }:

Post a Comment for "Multiple Conditions With While Loop In Python"