Aug-31-2017, 12:10 PM
Other solutions are by formatting the input and using lists. In the first example, we change the users response to all lower case (a common practice), so italy, Italy, iTaLy, etc would all evaluate to True. There are other options available, such as changing the input to all capital letters, but again, probably the most common is lower case.
The second way, with lists, checks if the users input exists in the list, if it does, it evaluates to True other wise False. You could combine the two methods so rather than having a list of all variants, you only need the lower case version. Asking for a favorite place, however, would result in a rather long list
The second way, with lists, checks if the users input exists in the list, if it does, it evaluates to True other wise False. You could combine the two methods so rather than having a list of all variants, you only need the lower case version. Asking for a favorite place, however, would result in a rather long list
# Changing any input to lowercase. Italy and italy both return True favoritePlace = input("how about your favorite place (using 'lower')?").lower() if favoritePlace == "italy": print("I love that place, same here") else: print("not bad") # Using a list places = ['italy', 'Italy'] favoritePlace = input("how about your favorite place (using list)?").lower() if favoritePlace in places: print("I love that place, same here") else: print("not in list") # Adding to your list places = ['italy', 'Italy'] favoritePlace = input("how about your favorite place (combining and adding)?").lower() if favoritePlace == "italy": print("I love that place, same here") else: print("I will add that to my list") places.append(favoritePlace) print("New list = ", places)
Output:C:\Python36\python.exe C:/Python/Games/scratch.py
how about your favorite place (using 'lower')?Italy
I love that place, same here
how about your favorite place (using list)?italy
I love that place, same here
how about your favorite place (combining and adding)?england
I will add that to my list
New list = ['italy', 'Italy', 'england']
Process finished with exit code 0
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition