Join 300,455 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,602 people online right now. Registration is fast and FREE... Join Now!
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
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.
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.
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
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
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