-1

I'm very new to programming and a bit lost so please forgive me if I don't explain myself well.

How can the user also input "y" or "yeah" for example and still be correct?

question = input("Does 1+1=2? ")

if question == "yes":
    print("Correct")
else:
    print("Incorrect")

Edit:

Thank you all so much for your answers! After reading your helpful comments this is what worked for me:

question = input("Does 1+1=2? ")

accepted_answers = {"yes", "y", "yeah"}

if question in accepted_answers:
    print("Correct")
else:
    print("Incorrect")
Jordan
  • 1
  • 1
  • 1
    Please follow a tutorial on booleans and if conditions in python. Stack Overflow is not a tutorial website. Please take the [tour], read [ask] and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and [How much research effort is expected of Stack Overflow users?](//meta.stackoverflow.com/a/261593/843953) Welcome to Stack Overflow! – Pranav Hosangadi Feb 04 '21 at 17:18

2 Answers2

1

If you want to accept anything that starts with a y or Y you could use

if question.lower().startswith("y"):
nullUser
  • 1,601
  • 1
  • 12
  • 29
0

Welcome to StackOverflow and to programming! In your example, you're asking a question and eliciting a response from the user:

question = input("Does 1+1=2? ")

What you're then looking for is the ability to be flexible in how you handle their response. As @pranav's comment mentions, you should read up on Booleans, Boolean logic, etc. AS your code notes, one acceptable response is question == "yes". But you might also check for question == "y" or question == "YES" or question == "yep!". Note my use of the word or here, which is how you can allow any one of those to be an acceptable condition. So in Python, you could say:

if question == "yes" or question == "y" or question == "YES" or question == "yep!":
    print("Correct")
else:
    print("Incorrect")

Many languages allow you to do this in an easier-to-read way, and it's also more idiomatic. For example:

if question in ["yes", "y", "YES", "yep!"]:
    print("Correct")
else:
    print("Incorrect")

And you can further improve your program by recognizing that the case of the response might vary (yes, YES, Yes). So you can address this in your code:

if question.lower() in ["yes", "y", "yep!"]:
    print("Correct")
else:
    print("Incorrect")

You can use the Python REPL to do interactive programming to find out what functions are available. For example:

>>> question = input("Does 1+1=2? ")
Does 1+1=2? yes
>>> question
'yes'
>>> type(question)
<class 'str'>
>>> dir(question)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> question.title()
'Yes'
user655321
  • 1,572
  • 2
  • 16
  • 33