def chama ():
a = str(input('Do you want to enter another word? If Yes (Y), No (N)')).lower()
if a == 'Y' or 'yes':
word = []
word.append(input('Please enter a word'))
string = ''.join(map(str,word))
inverse = string[::-1]
if string == inverse:
print ('''The word that you entered:''', string.upper(), '''is a palindrome''')
if a == 'N' or 'NO':
print ('See you next time!')
chama ()

- 20,090
- 4
- 28
- 58

- 27
- 4
-
1The second expression for the `or` is a string, which always evaluates to true for non-empty strings. You need it to be like `... or a == 'Yes'`. The same goes for the No. – Andrew Wei Oct 30 '21 at 06:41
-
But it still no showing the command – Jorge de Paula Jr Oct 30 '21 at 06:50
-
def chama (): a = str(input('Do you want to enter another word? If Yes (Y), No (N)')).lower() if a == 'Y' or a =='yes': word = [] word.append(input('Please enter a word')) string = ''.join(map(str,word)) inverse = string[::-1] if string == inverse: print ('''The word that you entered:''', string.upper(), '''is a palindrome''') if a == 'N' or a =='NO': print ('See you next time!') chama () – Jorge de Paula Jr Oct 30 '21 at 06:50
-
Additionally, since you convert your input to lowercase using `.lower()`, it will never be equal to an uppercase string. – buddemat Oct 30 '21 at 06:54
4 Answers
Well for if statements you need to have a proper comparison after the or
if a == 'Y' or a == 'yes':
You are also calling .lower()
which converts the characters to lowercase but then you are trying to compare to uppercase. So since you are converting the input to lowercase you need to make sure you are comparing to 'y' or 'yes'
and 'n' or 'no'
. This is good practice as it will allow for the user to type n N No nO NO no
and y Y yes Yes yEs YEs yeS YeS yES YES
This should resolve those problems.
def chama():
a = str(input('Do you want to enter another word? If Yes (Y), No (N)')).lower()
if a == 'y' or a == 'yes':
word = []
word.append(input('Please enter a word'))
string = ''.join(map(str,word))
inverse = string[::-1]
if string == inverse:
print ('''The word that you entered:''', string.upper(), '''is a palindrome''')
if a == 'n' or a == 'no':
print ('See you next time!')
chama()
If you want the program to repeat until the user enters no
I would recommend just putting a while loop at the top with a break
if the user types no
def chama():
while True:
a = str(input('Do you want to enter another word? If Yes (Y), No (N)')).lower()
if a == 'y' or a == 'yes':
word = []
word.append(input('Please enter a word'))
string = ''.join(map(str,word))
inverse = string[::-1]
if string == inverse:
print ('''The word that you entered:''', string.upper(), '''is a palindrome''')
else:
print ('''The word that you entered:''', string.upper(), '''is NOT a palindrome''')
if a == 'n' or a == 'no':
print ('See you next time!')
break
chama()
I modified the code above to have an else
statement that would let the user know if the entered word is NOT a palindrome

- 1,106
- 10
- 26
-
Man thanks for your help! But the program is stopping is not running as should. When ask if the user wants to continue, after add a word it doesn't give the result. def main (): string = (input('Please enter a word: ')) reversed = '' for i in string: reversed = i+reversed if string == reversed: print ('''The word that you entered:''', string.upper(), '''is a palindrome''') else: print ('''The word that you entered:''', string.upper(), '''is not a palindrome.''') main() chama() – Jorge de Paula Jr Oct 30 '21 at 08:07
-
@JorgedePaulaJr Well your program is setup to print the word only if it is a palindrome. What do you want it to do if the word is not a palindrome? – ConnerWithAnE Oct 30 '21 at 08:10
-
yes, like if is not a palindrome, the program should say print ('''The word that you entered:''', string.upper(), '''is not a palindrome.'''). It shoud keep showing when the user say yes... like if is a palindrome or if is not – Jorge de Paula Jr Oct 30 '21 at 08:17
If you write if a=="y" or "yes"
it will always return true
:-
>>> a=='y' or 'yes'
'yes'
>>> a="y"
>>> a=='y' or 'yes'
True
>>> a="n"
>>> a=='y' or 'yes'
'yes'
>>> bool('yes')
True
This is how you can get the required result:-
def chama ():
a = input('Do you want to enter another word? If Yes (Y), No (N): ').lower()
if a == 'y' or a == 'yes':
word = []
word.append(input('Please enter a word: '))
string = ''.join(word)
inverse = string[::-1]
if string == inverse:
print ('''The word that you entered:''', string.upper(), '''is a palindrome''')
if a == 'n' or a == 'no':
print ('See you next time!')
chama ()

- 1,579
- 1
- 5
- 18
Try to write the code like:
if a == 'y' or a == 'yes':
and
if a == 'n' or a == 'no':
Reason: The one mentioned by Bibhav in this post. If you write if a=="y" or "yes"
, it will always return true
.

- 888
- 1
- 8
- 20
Just write the if condition correctly and your code is ready to go. Just change the line:
if a=="Y" or "yes"
to if a=="Y" or a=="yes"
and the second line:
if a=="N" or "NO"
to if a=="N" or a=="NO"