Guest User

Untitled

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. ## Chapter 7 ##
  2.  
  3. ### 99 Bottles of Beer on the Wall
  4. SONG_BOTTLES = 99
  5. print "Lyrics of the song #{SONG_BOTTLES} Bottles of Beer\n\n"
  6.  
  7. remaining_bottles = SONG_BOTTLES
  8. while remaining_bottles > 0
  9. print "#{remaining_bottles} bottles of beer on the wall, " <<
  10. "#{remaining_bottles} bottles of beer.\n" <<
  11. "Take one down and pass it around, " <<
  12. "#{remaining_bottles -= 1} bottles of beer on the wall.\n\n"
  13. end
  14.  
  15. print "No more bottles of beer on the wall, no more bottles of beer.\n" <<
  16. "Go to the store and buy some more, #{SONG_BOTTLES} bottles of beer on the wall.\n"
  17. ### Deaf grandma
  18. MIN_YEAR = 1930
  19. MAX_YEAR = 1950
  20.  
  21. print "Hello kid, what do you want from grandma? "
  22.  
  23. request = gets.chomp
  24. while request.index("BYE").nil?
  25. if request != request.upcase
  26. print "HUH?! SPEAK UP, SONNY! "
  27. else
  28. year = rand((MAX_YEAR - MIN_YEAR) + 1) + MIN_YEAR
  29. print "NO, NOT SINCE #{year}! "
  30. end
  31. request = gets.chomp
  32. end
  33. ### Deaf grandma extended
  34. MIN_YEAR = 1930
  35. MAX_YEAR = 1950
  36. BYE_MAX_TRIES = 3
  37.  
  38. print "Hello kid, what do you want from grandma? "
  39.  
  40. bye_tries_in_a_row = 0
  41. while bye_tries_in_a_row < BYE_MAX_TRIES
  42. request = gets.chomp
  43. if request.index("BYE").nil?
  44. bye_tries_in_a_row = 0
  45. if request != request.upcase
  46. print "HUH?! SPEAK UP, SONNY! "
  47. else
  48. year = rand((MAX_YEAR - MIN_YEAR) + 1) + MIN_YEAR
  49. print "NO, NOT SINCE #{year}! "
  50. end
  51. else
  52. bye_tries_in_a_row += 1
  53. if bye_tries_in_a_row == BYE_MAX_TRIES
  54. break
  55. end
  56. print "I CAN'T HEAR YOU! "
  57. end
  58. end
  59. ### Leap years
  60. print "Hello there! We are going to get the leap years in a range, OK?\n"
  61. print "Starting year: "
  62. starting_year = gets.to_i
  63. print "Ending year: "
  64. ending_year = gets.to_i
  65.  
  66. leap_years = []
  67. current_year = starting_year
  68.  
  69. while current_year <= ending_year
  70. if current_year.remainder(4) == 0 &&
  71. (current_year.remainder(100) != 0 || current_year.remainder(400) == 0)
  72. leap_years.push current_year
  73. end
  74. current_year += 1
  75. end
  76.  
  77. if leap_years.length > 0
  78. puts "The leap years between #{starting_year} and #{ending_year} are:"
  79. puts leap_years
  80. else
  81. puts "There are no leap years between #{starting_year} and #{ending_year}."
  82. end
Add Comment
Please, Sign In to add comment