Learn to Program Example by Chris Pine

Here is a program I through together this morning. The only thing I haven’t gotten working is validating the month can be value.dowcase or value.upcase or value.capitalize

 

mo = {1 => “Jan”, 2 => “Feb”, 3 => “Mar”, 4 => “Apr”, 5 => “May”, 6 => “Jun”,

7 => “Jul”, 8 => “Aug”, 9 => “Sep”, 10 => “Oct”, 11 => “Nov”, 12 => “Dec”}

def ask string
  puts ‘What ‘ + string + ‘ were you born?’
end

ask “year”
year = gets.chomp
while (year.to_i > Time.now.year) || (year.to_i < Time.at(1875).year )
  puts ‘Please enter a 4 digit year between 1875 and ‘ + Time.now.year.to_s
  year = gets.chomp
end

 # need to get month working so that value, value.downcase, and value.upcase are working

ask “month”
month = gets.chomp
while (month.to_i<1) or (month.to_i > 12)
  puts ‘Please enter a month between 1 and 12’
  month = gets.chomp
end

ask “day”
day = gets.chomp
while (day.to_i < 1) or (day.to_i > 31)
  puts ‘Please enter a day of the month’
  day = gets.chomp
end

def spank (year, month, day)
  birthday = Time.local(year, month, day)
  years_old = (Time.now – birthday) / 3600 / 24 / 365.25
  puts ‘You get ‘ + years_old.floor.to_s + ‘ spankings!!!’
  puts “SPANK!!!\n” * years_old.floor
end

spank year, month, day

Advertisements

One thought on “Learn to Program Example by Chris Pine

  1. thought I would respond with an update. I fixed this program so that you may enter JAN or jan, or even Janbuberry. I also fixed the day so that you may only enter the correct amount of days pertaining to the month entered. I don’t like all the conditions written in the days statement but I’ve moved on.

    Here is the code if you wanted to view it: http://pastie.org/7948298

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s