dimanche 1 février 2015

'OR' Is Not Working When Checking If Something 'IS NOT' Or '!=' In a String?

Looked around for an answer, but couldn't find anything. Here's my code:



def translate(string, c = 0):
for char in string:
if char != 'u' or char != 'i':
print string[c]
c += 1

translate("this is fun")


If I try this, the whole if statement gets canceled out and it returns "t h i s i s f u n" vertically. But, if I do this if statement instead, it works perfectly. It only cancels the if statement if I have 2 or more conditions that are =!. Heres 2 if statements that work the way they are supposed to:



def translate(string, c = 0):
for char in string:
if char == 'u' or char == 'i':
print string[c]
c += 1

translate("this is fun")


And:



def translate(string, c = 0):
for char in string:
if char != 'u':
print string[c]
c += 1

translate("this is fun")


So, does OR only work with == and not !=? I have also tried putting it in English with is not.


Aucun commentaire:

Enregistrer un commentaire