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'