User input | Sololearn: Learn to code for FREE!


User input

I am creating a python application to prompt user to enter either their zip code or city. How can I create input with options either it will be zip code which would be integer int(input()) and for city would be str(input()). But how to give option to enter either or.

4/21/2022 12:54:18 PM

Knowledge Is Power

4 Answers

New Answer


When it comes to this, you have 2 options: 1. Make the user choose 2. Make your program recognize wheter the input is a number or a string I would suggest you the second one, as the first would need additional input management. Here's my example:


Both city name and zip code input can be read in string format. So there probably be no need to convert input string to integer. But the zip code format matters in this case. If a zip code consists of only digits then you can simply use <input_string>.isdigit() to verify all characters in <input_string> represent digits. It is unlikely (or rarely) for a city name to consists of digits only.


Thank you for great examples.