Guest User

Untitled

a guest
Jan 17th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. Exercises:
  2. 1. Discuss bad style Madlibs example
  3. Should change vairable names to something more descriptive; variable foodPlural
  4. should be changed to food_plural; more white space would make this easier to read;
  5. spacing on line 14 is off; noth sure about use of /n in the string out put at the end..
  6. is that necessary?; add code comments
  7.  
  8. 2. How would you modify the code below to improve the code style and readability?
  9. #Array containing foods
  10. foods = ["shrimp", "cauliflower", "salmon", "garlic", "oysters",
  11. "salami", "tomatoes", "okra", "zucchini", "avocado"]
  12.  
  13. print "\nHow many foods would you like to see? I suggest 10, but it's up to you. "
  14. items=gets.chomp.to_i
  15.  
  16. #prints different strings depending on user input
  17. if items >10
  18. puts "Best I can do is 10. Let's do 10."
  19. items=10
  20. elsif items >=1 && items<= 10
  21. puts "Ok! Let's do " + items.to_s + "."
  22. else
  23. puts "We'll just pretend you said 10."
  24. items = 10
  25. end
  26.  
  27. puts "...Your Fancy Random Menu..."
  28.  
  29. items.times do |i|
  30. y = foods[rand(0...(foods.length))]
  31. puts i.to_s+": "+y
  32. end
  33.  
  34. 3. Revisit your MadLibs code, and update your code to improve its style
  35. Done - added code comments
  36.  
  37. 4. Bio Program
  38.  
  39. Create a program that accepts input from the user and outputs a bio with that information
  40. Use up to five different attributes about the person to populate the bio
  41. Output should consist of a paragraph of output created from the users input
  42.  
  43. puts "Please enter the following information:/n"
  44.  
  45. #gets user input for bio
  46. puts "Name (first and last):"
  47. name = gets.chomp
  48.  
  49. puts "Parent(s) Name(s):"
  50. parent_names = gets.chomp
  51.  
  52. puts "Favorite color:"
  53. favorite_color = gets.chomp
  54.  
  55. puts "City born in:"
  56. birth_place = gets.chomp
  57.  
  58. puts "Do you prefer cats or dogs:"
  59. pet_lover = gets.chomp.downcase
  60.  
  61. if pet_lover == "dogs"
  62. pet_lover = "an amazing person because they think dogs are the best"
  63. else
  64. pet_lover = "a fine person, but could use better taste in animals since we all
  65. know dogs are the best"
  66. end
  67.  
  68. #Generates paragraph of bio information
  69. puts "#{name} is #{pet_lover}. #{name} was born in #{birth_place} to #{parent_names}.
  70. Their favorite color is #{favorite_color}."
Add Comment
Please, Sign In to add comment