-1

I write code.

if statement in function def.

i don't know why the answer always "yes"

suppose we type "n"

a = input('''Yes or No, Type [Y/N]''')

def test():
   global a
   if a == 'Y' or 'y':
      print("yes")
   elif a == 'N' or 'n':
    print("no")
   else:
    print("Not Yes and Not No")

test()
whoami
  • 141
  • 1
  • 9

3 Answers3

1

You can make it much simpler by making it not case sensitive

a = input('Yes or No Type [Y/N]')

def test():

    global a

    if a.lower() == 'y': #this will convert the string into lower case

        print("yes")

    elif a.lower() == 'n':

        print("no")

    else:

        print("Not Yes and Not No")

test()
MrKioZ
  • 515
  • 4
  • 14
0

Try this to make it more robust:

yes = ['Y' , 'y' , 'yes' , 'YES', 'Yes']
no = ['N' , 'n' , 'NO', 'No', 'no']

if answer in yes:
    print("yes")

if answer in no:
    print("no")
Benjamin Kolber
  • 146
  • 1
  • 8
0
yes = ['y', 'yes']
no = ['n', 'no']

if answer.lower() in yes:
    print("yes")

if answer.lower() in no:
    print("no")
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137