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.") endUpdate:
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
0 comments:
Post a Comment