School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,099 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,027 people online right now. Registration is fast and FREE... Join Now!




Birthday SPANK!

 

Birthday SPANK!

Inkman

10 Oct, 2008 - 11:29 AM
Post #1

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
I have an assignment to write a program that asks what month, day, and year you were born on, then return a birthday "SPANK!" for every birthday they have had. I am stumped. I left comments in the code.

Sorry, I am really no good at this at all(I lack the logical thinking part that eveyone is bragging about.). I chose the wrong profession, but I will try my best regardless.

CODE

puts 'What year were you born in?(1970 - 2008)'
yr = gets.chomp

# I don't know how to get these if statements to re-ask the questions, instead of going to the next question. else becomes an unexpected error, return doesn't work without a def (another thing I'm just about clueless at)so I am told, and while still confuses me.

if (yr.to_i  < 1970)  || (yr.to_i > 2008)
  puts 'Please enter a year after 1970, and before 2008'
  end
     puts 'What month were you born in?(1 - 12)'
     month = gets.chomp
      if (month.to_i  < 1)  || (month.to_i > 12)
        puts 'Please enter a valid month'
        end
        puts 'What day were you born on? (1 - 31)'
        day = gets.chomp
          if (day.to_i < 1) || (day.to_i > 31)
            puts 'Please enter a valid day for the month.'
          end

time1 = Time.mktime(yr, month, day)
time2 = Time.new
time3 = (31536000)
time4 = (time2 - time1).to_i
time5 = (time4 / time3).to_s

puts time5

# ok, it tells me my age, now I need to change the number into one "SPANK!" for every birthday.

# secs now - secs born = (secs lived) / ((((secs per min = 60) min per hr = 60) hr per day = 24) day per year = 365) = number of spanks


User is offlineProfile CardPM
+Quote Post


MitkOK

RE: Birthday SPANK!

10 Oct, 2008 - 10:01 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Aug, 2007
Posts: 356



Thanked: 19 times
Dream Kudos: 250
My Contributions
You write this code and don't know how to print n times Spank ( where n is time5 in your case ) ?
User is offlineProfile CardPM
+Quote Post

Inkman

RE: Birthday SPANK!

13 Oct, 2008 - 11:31 AM
Post #3

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
I thought I had posted my first code, but I hit preview post, so I went to try to figure it out, I had it working.. kinda, then my teacher came and added and changed a bunch of stuff. So, this is my program with my teachers touchup. Mine didn't look very nice, and had some bugs.

When I came to check the forum, I realized I never put last one up, so less questions were added.

I kinda get the idea of how this stuff works. but, I still dont know how to get it to put spank for every number. I have tried bunch of stuff, it either doesn't work, or comes up with a zero.

I also got another problem. If I answer one of the questions wrong, It asks me to put a correct time, then goes to the next question. I will try to figure out, but if you answer, that would be cool too. I think I could use a return, but I'm not sure how those work yet.

(told you, I am no good at this.)
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Birthday SPANK!

13 Oct, 2008 - 11:40 AM
Post #4

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 14,931



Thanked: 517 times
Dream Kudos: 11550
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Just a tip, instead of using

ruby

if (yr.to_i < 1970) || (yr.to_i > 2008)


I would make it more synamic so it doesnt have to be changed each yer, something like

ruby

t = Time.now

if(yr.to_i < 1970) || (yr.to_i > t.year)


That way this would work for any year. Just my 2¢ smile.gif
User is offlineProfile CardPM
+Quote Post

MitkOK

RE: Birthday SPANK!

13 Oct, 2008 - 11:58 PM
Post #5

D.I.C Regular
Group Icon

Joined: 9 Aug, 2007
Posts: 356



Thanked: 19 times
Dream Kudos: 250
My Contributions
@Inkman
QUOTE(MitkOK @ 11 Oct, 2008 - 09:01 AM) *

You write this code and don't know how to print n times Spank ( where n is time5 in your case ) ?


@PsychoCoder
You can use directly Time.now.year, instead of accessing it later:
ruby

t = Time.now.year
if(yr.to_i < 1970) || (yr.to_i > t)


Another way to do it is with range :
ruby
time_limit = 1970..Time.now.year
if time_limit.include? yr
...



User is offlineProfile CardPM
+Quote Post

Inkman

RE: Birthday SPANK!

14 Oct, 2008 - 09:36 AM
Post #6

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
Well, thanks for the tips, I was just skimming through the book I have, and found out how to make my program do what I ask of it, It still doesn't reask the question when I put in a wrong answer, I will try your little tips, see what that does. Thanks again.
User is offlineProfile CardPM
+Quote Post

Inkman

RE: Birthday SPANK!

14 Oct, 2008 - 10:33 AM
Post #7

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
This is what I have now, basically the exact same thing, I just wrote this one all by myself, I did however get this to reask the question, although it will go through the whole program, then it will ask for whatever one is wrong, then give me an answer.

Some tips on shortening this mess would be cool too.

CODE
puts 'What year were you born? (1970 - 2008)'
  yr = gets.chomp
  
puts 'What month were you born? (1 - 12 )'
  mth = gets.chomp
  
puts 'What day were you born? (1 - 31)'
  day = gets.chomp
  
    
if (yr.to_i < 1970 .. yr.to_i > 2010)
puts 'Please enter a valid year.'
    puts 'What year were you born? (1970 - 2008)'
      yr = gets.chomp
        end

if (mth.to_i < 1 .. mth.to_i > 12)
puts 'Please enter a valid month.'
    puts 'What month were you born? (1 - 12 )'
      mth = gets.chomp
        end

if (day.to_i < 1 .. day.to_i > 31)
puts 'Please enter valid day'
    puts 'What day were you born? (1 - 31)'
      day = gets.chomp
        end

t1 = Time.mktime(yr, mth, day)
t2 = Time.now
t3 = (t2 - t1).to_i
t4 = (31536000)
t5 = (t3 / t4).to_s

puts 'SPANK! ' *t5.to_i

User is offlineProfile CardPM
+Quote Post

MitkOK

RE: Birthday SPANK!

14 Oct, 2008 - 11:33 AM
Post #8

D.I.C Regular
Group Icon

Joined: 9 Aug, 2007
Posts: 356



Thanked: 19 times
Dream Kudos: 250
My Contributions
The simple way :
ruby
year, month, day=0, 0, 0 
y,m, d = 1970..Time.now.year, 1..12, 1..31

until y.include? year
puts 'What year were you born?'
year = gets.chomp.to_i
end

until m.include? month
puts 'What month were you born?'
month = gets.chomp.to_i
end

until d.include? day
puts 'What day were you born?'
day = gets.chomp.to_i
end

t1 = Time.mktime(year, month, day)
t2 = Time.now
t3 = (t2 - t1).to_i
t4 = (31536000)
t5 = (t3 / t4).to_s

puts 'SPANK! ' *t5.to_i
# or (1..t5.to_i).each { puts "Spank" }


This post has been edited by MitkOK: 14 Oct, 2008 - 11:43 AM
User is offlineProfile CardPM
+Quote Post

Inkman

RE: Birthday SPANK!

14 Oct, 2008 - 12:10 PM
Post #9

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
Wow, that is cool. Too, bad I will never be able to write code like that sad.gif.

Thanks, you introduced "until". I didn't know about that.

also, the year, month, day=0, 0, 0. What is that for? is it sort of like def? Simply a variable? or does the computer know what they are already?

lastly, what is include?

If you don't mind me asking of course.

Thanks.
User is offlineProfile CardPM
+Quote Post

MitkOK

RE: Birthday SPANK!

14 Oct, 2008 - 01:49 PM
Post #10

D.I.C Regular
Group Icon

Joined: 9 Aug, 2007
Posts: 356



Thanked: 19 times
Dream Kudos: 250
My Contributions
I must say that is not the best code, but it's working. Everyone can learn to program Ruby, because it's easy and mostly it's fun wink2.gif

until is the opposite of while, as unless is opposite of if in Ruby :
ruby
n = 0
while n < 10
n+=1
puts n
end

# is the same as

until n > 9
n+=1
puts n
end

year,month, day = 0,0,0 is multiple assignment, instead of:
ruby
year = 0
month = 0
day = 0

include is a method that check if passed object is member of receiver range object.
In your case it's checking if the entered year/month/day is included in the range.

This post has been edited by MitkOK: 14 Oct, 2008 - 01:51 PM
User is offlineProfile CardPM
+Quote Post

Inkman

RE: Birthday SPANK!

15 Oct, 2008 - 09:29 AM
Post #11

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 11


My Contributions
Thanks, you have been a big help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:09PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month