Advertisement
dougllio

Updated Cities/List example

Apr 27th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. '''
  2. This Python 2 program will build a list of favorite
  3. cities based on input from the user, then compare the
  4. cities to my list of favorite cities and output the
  5. similarities. It then offers to delete a city from
  6. the list and show the results.
  7.  
  8. UPDATED: refactor using sets, index, ...
  9. '''
  10.  
  11. # Initialize my list with my favorite cities
  12. myCities = ['New York','Los Angeles','London','Paris']
  13.  
  14. # Print out my favorite cities
  15. print "My favorite cities are: ",myCities
  16. print ""
  17.  
  18. # Ask the user for the number of cities they want to enter
  19. yourCityCount = raw_input ("How many favorite cities do you have?: ")
  20.  
  21. # Validate that the input is a positive number
  22. while not(yourCityCount.isdigit()):
  23.   yourCityCount = raw_input ("How many favorite cities do you have? (NUMBERS ONLY): ")
  24.  
  25. # Change type to integer
  26. yourCityCount = int(yourCityCount)
  27.  
  28. # Setup an empty list to be appended to by the user
  29. yourCities=[]
  30.  
  31. # Loop through based on the user's number of cities and add their city to their list
  32. # x is the loop counter
  33. x=1
  34. while x<yourCityCount+1:
  35.   # Get the name of the city
  36.   city = raw_input("Enter your number " + str(x) + " favorite city: ")
  37.  
  38.   # Make sure it's not a duplicate. If it's not, add it to the list and increase the loop counter
  39.   if city in yourCities:
  40.     print "You already entered", city + "! Please try again."
  41.   else:
  42.     x+=1
  43.     yourCities.append(city)
  44.  
  45. # Output the user's list of cities
  46. print ""
  47. print "You have",str(yourCityCount) + " favorite cities: "
  48. print yourCities
  49. print ""
  50.  
  51. # Compare the user's list of cities to see which cities are in common with my list
  52. # Keep a count to display at the end
  53. sameCities = set(yourCities).intersection(set(myCities))
  54. sameCityCount = len(sameCities)
  55.  
  56. # Display the number of cities in common, with correct plural noun
  57. if sameCityCount > 0:
  58.   print "Cities we both like:", ", ".join(sameCities)
  59.   if sameCityCount > 1:
  60.     print "We have",str(sameCityCount) + " cities in common."
  61.   else:
  62.     print "We have",str(sameCityCount) + " city in common."
  63. else:
  64.   print "We have no cities in common."
  65.   print ""
  66.  
  67. # Offer to delete a city. Only attempt if there are cities
  68. if yourCityCount > 0:
  69.  
  70.   # Get the name of the city to delete
  71.   cityToDelete = raw_input("Which city would you like to delete?: ")
  72.  
  73.   # Only proceed if the city is in the list
  74.   if cityToDelete in yourCities:
  75.  
  76.     # If this item to delete, pop it
  77.     yourCities.pop(yourCities.index(cityToDelete))
  78.  
  79.     # Print out the new list of cities
  80.     print "I have deleted", cityToDelete + " from your list:", yourCities
  81.   else:
  82.     # Print an error message
  83.     print "I couldn't find", cityToDelete + " in your list."
  84.  
  85. # Exit
  86. raw_input ("Press Enter to Exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement