Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exercises:
- 1. Discuss bad style Madlibs example
- Should change vairable names to something more descriptive; variable foodPlural
- should be changed to food_plural; more white space would make this easier to read;
- spacing on line 14 is off; noth sure about use of /n in the string out put at the end..
- is that necessary?; add code comments
- 2. How would you modify the code below to improve the code style and readability?
- #Array containing foods
- foods = ["shrimp", "cauliflower", "salmon", "garlic", "oysters",
- "salami", "tomatoes", "okra", "zucchini", "avocado"]
- print "\nHow many foods would you like to see? I suggest 10, but it's up to you. "
- items=gets.chomp.to_i
- #prints different strings depending on user input
- if items >10
- puts "Best I can do is 10. Let's do 10."
- items=10
- elsif items >=1 && items<= 10
- puts "Ok! Let's do " + items.to_s + "."
- else
- puts "We'll just pretend you said 10."
- items = 10
- end
- puts "...Your Fancy Random Menu..."
- items.times do |i|
- y = foods[rand(0...(foods.length))]
- puts i.to_s+": "+y
- end
- 3. Revisit your MadLibs code, and update your code to improve its style
- Done - added code comments
- 4. Bio Program
- Create a program that accepts input from the user and outputs a bio with that information
- Use up to five different attributes about the person to populate the bio
- Output should consist of a paragraph of output created from the users input
- puts "Please enter the following information:/n"
- #gets user input for bio
- puts "Name (first and last):"
- name = gets.chomp
- puts "Parent(s) Name(s):"
- parent_names = gets.chomp
- puts "Favorite color:"
- favorite_color = gets.chomp
- puts "City born in:"
- birth_place = gets.chomp
- puts "Do you prefer cats or dogs:"
- pet_lover = gets.chomp.downcase
- if pet_lover == "dogs"
- pet_lover = "an amazing person because they think dogs are the best"
- else
- pet_lover = "a fine person, but could use better taste in animals since we all
- know dogs are the best"
- end
- #Generates paragraph of bio information
- puts "#{name} is #{pet_lover}. #{name} was born in #{birth_place} to #{parent_names}.
- Their favorite color is #{favorite_color}."
Add Comment
Please, Sign In to add comment