-1

I am trying to create a small bit of code in Python that runs a conversation, then asks a Y/N question i.e "Do you believe the sky is blue?"

When I run the code, it works well until I reach the question. It ignores my parameters for Y/N answers to print specific responses. It gives me the print response attached to my "else" statement.

I am confused if I am writing my If/elif/else statements wrong?

My code is written as follows:

x = input('do you believe the sky is blue')
if x == "yes":
   print("I believe you are correct")
elif x == "no":
   print('I think you have a unique perspective.')
else:
   print('Please answer Yes or No.)
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 2
    If you are typing in "Yes" or "No", then your if and your elif statements will not hit, because they are case sensitive. – Shmack Jan 08 '23 at 03:34
  • Check how I just changed the code formatting -- and try to do it that way yourself in the future. – Charles Duffy Jan 08 '23 at 03:35
  • \`\`\` marks the beginning of a code block, do the same to finish the code block. – Shmack Jan 08 '23 at 03:35
  • As for the question, though, because you aren't showing us what input you type when running your program we can't speak to its behavior. – Charles Duffy Jan 08 '23 at 03:36
  • 1
    What are you actually typing as the answer? – John Gordon Jan 08 '23 at 03:36
  • @DaviASampaio, why the proposed edit to switch from three-backtick blocks to four-space-indents? – Charles Duffy Jan 08 '23 at 03:37
  • @Superhots, ...if the problem is that you're entering `Yes` but your program accepts only `yes`, that would make the question a duplicate of [How do I do a case-insensitive string comparison in Python?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – Charles Duffy Jan 08 '23 at 03:38
  • @CharlesDuffy I made this change before (probably at the same time) the three-backtick edit. No need to change it. – Davi A. Sampaio Jan 08 '23 at 03:39

1 Answers1

-1

You use .lower() to obtain a lower case version of a string, and you are missing a ' in the last print():

x = input('do you believe the sky is blue ').lower()
if x == "yes":
   print("I believe you are correct")
elif x == "no":
   print('I think you have a unique perspective.')
else:
   print('Please answer Yes or No.')
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • If this is the problem, the question should be closed as a duplicate of https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison (or one of the many others on the topic), not answered. In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section _Answer Well-Asked Questions_, and the bullet point therein regarding questions that "have been asked and answered many times before". – Charles Duffy Jan 08 '23 at 03:39
  • @CharlesDuffy I didn't look at the initial version but it's probably significant white space, typo, and case sensitive string compare. – Allan Wind Jan 08 '23 at 03:44
  • Likely. Typo-based questions shouldn't be answered either, though, per #2 in the "some questions are still off-topic" list in https://stackoverflow.com/help/on-topic, and the bullet point in "How to Answer" that references the on-topic page. – Charles Duffy Jan 12 '23 at 20:55