Home > Net >  Why my program is not reading if else command?
Why my program is not reading if else command?

Time:10-30

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 ()

CodePudding user response:

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

CodePudding user response:

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 ()

CodePudding user response:

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.

CodePudding user response:

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"

  • Related