This blog no longer exists.
You will be redirected to something cool.

Friday, October 29, 2010

Quiz Program (Loops)

This one was difficult for me, but not as much as the last time I encountered loops. I think after a few more small programs with loops I'll feel more comfortable with this. I created a quiz about me. Feel free to run it and take it. (It's in Ruby.)


puts( '     <(^.^)> LITTLE KNOWN FACTS O\' MELANIE <(^.^)>')
puts
puts( 'To play, select the letter that corresponds with your answer
then press return')
answer = ''

while answer != 'c'
puts( '#1 Gym class sux. How would Melanie get out of running laps in Elem school?')
puts
puts( 'a. She got a doctors note')
puts( 'b. She refused')
puts( 'c. She hid')
puts( 'd. She got extra help in another subject instead of going to gym')
answer = gets.chomp.downcase
end

puts( 'Correct!')

answer = ''

while answer != 'b'
puts( '#2 Melanie has a scar on her knee from kindergarten. How did she get it?')
puts
puts( 'a. She fell off the jungle gym')
puts( 'b. She was running in flip flops and tripped')
puts( 'c. She got beat up')
puts( 'd. Her brother threw something at her')
answer = gets.chomp.downcase
end

puts( 'Correct!')

answer = ''

while answer != 'a'
puts( '#3 Melanie failed ONE class in high school. Which was it?')
puts
puts( 'a. government')
puts( 'b. business services technology')
puts( 'c. geometry')
puts( 'd. art')
answer = gets.chomp.downcase
end

puts( 'Correct!')

answer = ''

while answer != 'c'
puts( '#4 Melanie is l337 fail at sports. Which sport did she play in high school?')
puts
puts( 'a. basketball')
puts( 'b. cheerleading')
puts( 'c. track')
puts( 'd. volleyball')
answer = gets.chomp.downcase
end

puts( 'Correct!')

answer = ''

while answer != 'b'
puts( '#5 Melanie went on a family vacation when she was 10. Where did she go?')
puts
puts( 'a. Virginia Beach, VA')
puts( 'b. Chattanooga, TN')
puts( 'c. Groton, CT')
puts( 'd. Tell City, IN')
answer = gets.chomp.downcase
end

puts( 'Correct!')

Thursday, October 28, 2010

Random Number Game

It was a little difficult to come up with how to assign the random number to the variable -- it was a durrr moment for sure. So without further ado here is the first part of the program (or problem #2 according to that post I linked to above:)
randomNumber = rand(10)
randomNumber = randomNumber + 1
print( 'Guess my number! Enter a number (1-10): ')
userNumber = gets.chomp()
puts( "Your number is #{userNumber}. My number is #{randomNumber}.
This program is not yet advanced enough to tell you whether you
are correct -- but you should be able to tell.")

Here is problem #3:
# Try to figure out if there is a way to chomp & convert to an integer on
# same line (lines 7 and 8)
# Try to figure out how to print failure message (lines 14-16 & 20-22) for
# both cases in which the user is incorrect without having it in the program
# twice. 

randNumber = rand(10)
randNumber = randNumber + 1
print( 'Guess my number! Enter a number (1-10): ')
userNumber = gets.chomp.to_i()
if userNumber == randNumber
 puts( 'You guessed correctly! You are SOOO smart!')
elsif userNumber > randNumber
 puts( "Your number (#{userNumber}) is higher than my number (#{randNumber}.") 
 print( "You are unable to guess again because you are such a failure. It
has nothing to do with my inability to program this such that you could
guess again.")

else
 puts( "Your number (#{userNumber}) is lower than my number (#{randNumber}.)")
 print( "You are unable to guess again because you are such a failure. It
has nothing to do with my inability to program this such that you could
guess again.")

 end
Update:
I figured out how to fix problem #3 for that error message. Dumbest thing ever... how about a variable? I'm sure we all have those blonde moments, though. Also, got help from a friend on the chomp.to_i thing (that's what you do>> gets.chomp.CONVERSION NOT gets.CONVERSION.chomp.) New code:
randNumber = rand(10)
randNumber = randNumber + 1
print( 'Guess my number! Enter a number (1-10): ')
userNumber = gets.chomp.to_i()
failureMessage = 'You are unable to guess again because you are such a failure. It
has nothing to do with my inability to program this in such a way that you 
could guess again.'
if userNumber == randNumber
 puts( 'You guessed correctly! You are SOOO smart!')
elsif userNumber > randNumber
 puts( "Your number (#{userNumber}) is higher than my number (#{randNumber}.") 
 print failureMessage

else
 puts( "Your number (#{userNumber}) is lower than my number (#{randNumber}.)")
 print failureMessage

 end

Problem #5 was much easier to complete and more fun as I'm getting the hang of loops:
randNumber = rand(10)
randNumber = randNumber + 1
count = 0
userNumber = ()
print( 'Guess my number! Enter a number (1-10): ')

while userNumber != randNumber
  userNumber = gets.chomp.to_i()
  count = count + 1
  failureMessage = 'You did not guess correctly. Guess again:'

  if userNumber == randNumber
 puts( "You guessed correctly! It took you #{count} time(s) to guess.")
  
  elsif userNumber > randNumber
 puts( "Your number (#{userNumber}) is higher than my number.")
 print failureMessage

  else
 puts( "Your number #{userNumber} is lower than my number.")
 print failureMessage

  end
end

Getting in the Groove (Embedded Variables)

Yeah, I'm still on the whole embedded variables thing, but this is SOOOOOO much easier. Today I worked on getting used to doing things this way. It'll take a bit of time to memorize, but in the meantime I created a mad lib to kind of get used to it. Enjoy!

#  Is it customary to start variable counts at one? (ie. noun1) or
#  at zero (ie. noun and the second being noun1) or how else is this
#  handled?

print( 'Enter an adjective: ' )
adjective1 = gets.chomp()
print( 'Enter a verb ending in "ed": ')
verbEd = gets.chomp()
print( 'Enter a plural noun: ')
pluralNoun1 = gets.chomp()
print( 'Enter a liquid: ')
liquid = gets.chomp()
print( 'Enter another plural noun: ')
pluralNoun2 = gets.chomp()
print( 'Enter a famous person: ')
famousPerson = gets.chomp()
print( 'Enter a place: ')
place = gets.chomp()
print( 'Enter an occupation: ')
occupation = gets.chomp()
print( 'Enter a noun: ')
noun1 = gets.chomp()
print( 'Enter a nationality (ie. American or Irish): ')
nationality = gets.chomp()
print( 'Enter a female celebrity: ')
femaleCelebrity = gets.chomp()
print( 'Enter another noun: ')
noun2 = gets.chomp()
print( 'Enter the name of a female friend: ')
femaleFriend = gets.chomp()
print( 'You\'re almost done. Enter another plural noun: ')
pluralNoun3 = gets.chomp()
print( 'Enter a number: ')
number = gets.chomp()
print( 'Enter an adjective: ')
adjective2 = gets.chomp()

puts ( 'Personal Ad:')
puts
puts ( "I enjoy long, #{adjective1} walks on the beach, getting #{verbEd} in 
the rain and serendipitous encounters with #{pluralNoun1}.
I really like piƱa coladas mixed with #{liquid}, and romantic, candle-lit
#{pluralNoun2}. I am well-read from Dr. Seuss to #{famousPerson}. I travel
frequently, especially to #{place}, when I am not busy with work. (I am a
#{occupation}.) I am looking for #{noun1} and beauty in the form of a 
#{nationality} goddess. She should have the physique of #{femaleCelebrity}
and the #{noun2} of #{femaleFriend}. I would prefer if she knew how to
cook, clean, and wash my #{pluralNoun3}. I know I’m not very attractive in
my picture, but it was taken #{number} days ago, and I have since become 
more #{adjective2}. ")

Embedded Variables in Strings

So I'm learning Ruby again, but this time I've got a program in mind. It will take a lot of brushing up and general learning before I will be able to start the program. I already know how to write the program without an interface, but I want to build an interface too, so it'll take a bit of work. I think once I get this particular program coded that I'll know enough about programming to be able to work on other programs so that will be really nice.

Tonight I started reading "The Book of Ruby" which is a big step up from the last book I read, but I think I'm understanding it fairly well considering the difference in difficulty between the two texts.

So tonight's topic was Strings and Embedded Evaluation. The sample code in the lesson is as follows:

print( 'Enter your name: ' )
name = gets()
puts( "Hello #{name}" )

After reading the OTHER book on Ruby I would have programmed this like so:
puts 'Enter your name:'
name = gets.chomp
puts 'Hello ' + name + '.'

However, I learned that by using "print" instead of puts, I can keep the cursor on the same line... which is cool, I don't know what real difference it would make other than aesthetics, but I guess I'll find out later. I did add a period so that I could make it a proper sentence and added the corresponding chomp to make this possible.

What I mostly learned was this whole embedded variables thing. I'm a little confused as to the purpose -- if it's just faster to code like this and less confusing than all the plusses and whatnot from something like 'Words here' + someVariable + 'another string.' I can really see that being the case, however, I'm not going to assume that it is simply for cleaner and faster programming -- until yeah, I am told so.

I also played a bit with "\n" and "\t" which are newline and tab. It took a bit getting used to the whole new way of doing the embedded thing and playing with newline and tab helped me get used to it a bit. Here's the code I created using this newfangled stuff I learned:

# Playing with tab \t and newline \n
puts ( 'How about a sentence of sort?')
sentence = gets()
puts ( "\t #{sentence}\t\t #{sentence}")

I used the sentence "I like rocks and giraffes." (Without the quotes.) And thus, the program spits out this:
I like rocks and giraffes.
     I like rocks and giraffes.
          I like rocks and giraffes.

Definitely some stuff to play around with for a bit. I perhaps I could rewrite this to capitalize the first letter if it is not capitalized and to add a period if there is not one. I think this would be a good project for tomorrow. :P