Advertisement
dougllio

TWIT Coding 101 - Python Day 2 Homework

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