0

I have written this very simple code. I am new to programming. But I seem to dont understand why this loops doesnt break when I give it "N" for an answer when I run it.

while (True):
    name = input("What is your name?\n")
    print(f"Hello {name}, how are you today?")
    answer = input("would you like to continue with the conversation? Reply with 'Y' or 'N'\n")
    if answer == "y" or "Y":
        continue
    elif answer == "N" or "n":
        break
    else:
        print("Kindly only answer with 'Y' or 'N'")

I wanted this to get out of loop and break the program when I enter "N"

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

You need to include answer == in both sides of the conditionals.

i.e.

while (True):
    name = input("What is your name?\n")
    print(f"Hello {name}, how are you today?")
    answer = input("would you like to continue with the conversation? Reply with 'Y' or 'N'\n")
    if answer == "y" or answer == "Y":
        continue
    elif answer == "N" or answer == "n":
        break
    else:
        print("Kindly only answer with 'Y' or 'N'")
Leshawn Rice
  • 617
  • 4
  • 13