Page 1 of 1

Python: Checking if Variable in List

Posted: Sun Sep 21, 2025 7:53 am
by The Jason

Code: Select all

list_of_some_Virginia_cities = list(("Virginia Beach", "Richmond", "Norfolk", "Norton", "Roanoke", "Bristol"))

try:

	your_choice_of_Virginia_city = input("Name a city in Virginia.")

except ValueError:

	return "Please pick a string value."

else:

	if your_choice_of_Virginia_city in list_of_some_Virginia_cities:

		print(f "Your choice is on the list")

	else:

		print(f "Your choice isn't on the list but might still be a Virginia city.")