Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I say, 'if the location is a zip code', or, 'if the location is a city name'?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading