I’m writing a program using the API from weather map, where I’m telling the user to enter a zip code or a city name, how do I program it to differentiate between a city name, or a zip code (which will be five digits)?
The program is being written in Python.
Thank you!
>Solution :
Assuming that there aren’t any cities with five digits as their name in the API output, you could do the following:
def is_zip_code(s):
# Returns true if s is a string of five digits, False otherwise.
return len(s) == 5 and s.isnumeric()