How to write more than one text inside of a variable [closed]

Tags: , , , ,



Hey Guys im new to Python and programming in general and i was creating little guessing game so i decleared a variable called answer code====> Answer = (‘Because We Are Toghether’) so it’s all working but i have a problem if someone tried to write the answer but in lower case or for example (Bec we are toghether) it will not work so how to fix this to accept the answer in every possible form? and please tell me how to Add more answers in the same variable For Example Answer = (‘Because We Are Toghether’) and (‘…..’) how to add another answer like this?

Answer

You want to clean the input before comparing.

Furthermore, you might want to think about “every possible form”. What if there’s a spelling error? What’s the minimum that needs to match between input and correct answer?

So let’s assume your input is a string, you want to compare to an answer.

possible_answers = ['because we are together', 'apple', 'one more possible answer']
input_to_test = 'Because we are together'
if input_to_test.lower() in possible_answers:
  print('Jay!')
else:
  print('Nope!')

In this test the string 'Because we are together' will be correct as well as the string 'Because We Are TogEther'as they are converted to lowercase. If you want to compare having more rules, you could have a look at regular expresions and more complex string matching.

If you want to use not exactly matching strings but somethin “close enough” like in your example: “Cause they are together” you might find fuzzy logic string matching helpful. There’s a simple introduction e.g. here: https://towardsdatascience.com/fuzzy-string-matching-in-python-68f240d910fe?gi=57b1ce825f01



Source: stackoverflow