User input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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.

21st Apr 2022, 12:54 PM
Knowledge Is Power
Knowledge Is Power - avatar
3 Answers
+ 1
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: https://code.sololearn.com/cNAs9BhSRUz1/?ref=app
21st Apr 2022, 1:09 PM
OrHy3
OrHy3 - avatar
+ 1
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.
21st Apr 2022, 1:18 PM
Ipang
0
Thank you for great examples.
21st Apr 2022, 1:16 PM
Knowledge Is Power
Knowledge Is Power - avatar