Python: Checking if Variable in List

Post Reply
User avatar
Jason
Site Admin
Posts: 809
Joined: Tue Jul 01, 2025 10:13 am

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.")
 

POSTREACT(ions) SUMMARY

Post Reply