CityId Mini Project
A Starter Template
The main idea of the project is to be able to find a zip code to a certain city with a user input. It’s a great way to just practice the basics.
City Info
Is a hash with a list with the names of cities & their zip codes.
city_info = {"lilburn" => "30047","norcross" => "30003","lawrenceville" => "30044","dacula" => "30019","alpharetta" => "30009","decatur" => "30030","loganville" => "30052","marietta" => "30060","roswell" => "30076","auburn" => "30011"}
The next thing I did was create a method to get the city by name.
def get_city_name(somecity)end
Here I would need to figure out a way to list out all the keys.
I then created another method to get the zip code and return a city associated with it.
def get_zip_code(somecity, key)end
Here I need to figure out how to return the key value hash that will be passed in.
So far I have two methods. One to get the city and the other to get the zipcode.
I used a basic loop. Infinite loops are not a good way to do it. However, since this is just a starter template I have used a basic
loop do
end
I added a puts statement. Asking for users input.
loop doputs "Do you want to lookup an area code based on a city name?(Y/N)"answer = gets.chomp.downcasebreak if answer != "y"puts "Which city do you want to lookup the zip code for?"
This next line of code calls the method ‘get_city_name’ which takes in the ‘city info’(the list of cities & zip). Then asks for users input.
puts get_city_name(city_info)
puts "Enter your choice"prompt = gets.chomp
I’ve assigned a variable prompt to user input.
In a case where a user inputs an invalid city, I have created an If Else Statement.
if city_info.include?(prompt)puts "The zip code for #{prompt} is #{get_zip_code(city_info, prompt)}"elseputs "you entered an invalid city name"endend
This will take in the prompt and then interpolate the zip code associated with the ‘city info’ hash and given prompt.
In the end it should look like this:
city_info = {"lilburn" => "30047","norcross" => "30003","lawrenceville" => "30044","dacula" => "30019","alpharetta" => "30009","decatur" => "30030","loganville" => "30052","marietta" => "30060","roswell" => "30076","auburn" => "30011"}#create a method to get city_by_namedef get_city_name(somecity) somecity.keys
#.keys method lists out all the keys
end#create a method to get the zip and return a city associated with itdef get_zip_code(somecity, key)somecity[key] #returning the value hash passed in and key value.end#use execution flow#infinite loop-no buenoloop doputs "Do you want to lookup an area code based on a city name?(Y/N)"answer = gets.chomp.downcasebreak if answer != "y"puts "Which city do you want to lookup the zip code for?"puts get_city_name(city_info)#now enter a selection and get inputerputs "Enter your choice"prompt = gets.chomp#chance User enters invalid city, let user know if/elseif city_info.include?(prompt)puts "The zip code for #{prompt} is #{get_zip_code(city_info, prompt)}"elseputs "you entered an invalid city name"endend