Case insensitive user input strings
In your string comparison, the correct answer itself had the first letter capitalized.
correctAnswer = "london"
userGuess = input("What is the capital of Great Britain?: ").lower()
if userGuess == correctAnswer:
print("Correct!")
else:
print("Wrong")
How do I make the input case-insensitive?
Use .lower() or .upper() or [.lower() and .capatalize()] according to how you store your months in the list.
How to make user input not case sensitive in Python?
You can convert the string to lower or upper case before doing the comparison.
if action.lower() == "throw it back into the ocean"
What is the proper way to allow a capitalized "Yes" input and a non-capitalized "yes" input or value in Python?
Try something like this:
x = input('...')
if(x == 'Yes' or x == 'yes'):
print('True')
else:
print('False')
How to make user-input for Symbols case insensitive
If you are wanting your input to be recast to lowercase so the derivatives that you hard coded always work you could either convert the input to lower case or, more safely, provide locals
that remap the uppercase symbols of interest to lower case symbols. For clarity in the following I use S
to sympify rather than the parser:
>>> S('x', {'x':'upper'}) # example showing you can replace 'x' with 'upper'
upper
>>> S('X', dict([(str(i), str(i).lower()) for i in symbols('X:Z')]))
x
In your code you will have to update your locals with the dict() that is being used in my example: loc = locals(); loc.update(dict([(str(i), str(i).lower()) for i in symbols('X:Z')]))
and use loc
instead of locals()
in your code.
Related Topics
Get Discord User Id from Username
How to Put a Space Between Two String Items in Python
List Available Font Families in 'Tkinter'
How to Count the Number of Messages
How to Mention a User in Discord.Py
How to Convert Datetime to Integer in Python
Best Way to Get the Max Value in a Spark Dataframe Column
Using Python, How to Access a Shared Folder on Windows Network
Python, Delete Json Element Having Specific Key from a Loop
How to Allocate Array With Shape and Data Type
Replacing Blank Values (White Space) With Nan in Pandas
How to Count Occurrences of Key in List of Dictionaries
Reduce Multi-Index/Multi-Level Dataframe to Single Index, Single Level
Comparing Items in Lists Within Same Indices Python
Passing Multiple Arguments from Django Template Href Link to View