Why do I keep getting "nil"?

R

Rick Barrett

I had a homework assignment where I had to create a number guessing game
using custom methods. I got it to work...but after some lines it'll put
"nil" on the next line and continue with the program.

ex. after an input

Please enter a number.
4
nil
Too low. Try again.
Please enter a number.
7
nil
Too low. Try again.

or after a generated line

You guessed the secret number in X tries!
nil

Why is it saying "nil" everywhere and how do I stop it?

My code is below if that helps...






# Methods ######################
def get_user_name()
puts "What is your name?"
name = gets().chomp().capitalize()
end

def num_eval(guess, number)

if (guess > 100)
puts "That's more than 100! Pick a number between 1 and 100!"
elsif (guess < 1)
puts "That's less than 1! Pick a number between 1 and 100!"
elsif (guess > number)
puts "Too high! Try again!"
elsif (guess < number)
puts "Too low! Try again!"
end
end

def get_user_guess(number)

guess = 0
num_guess = 0

while (guess != number) do
puts "Please enter a number."
guess = gets().chomp().to_i()
puts num_eval(guess, number)

num_guess = num_guess + 1
end

puts "Congrats you won!"
puts "You guessed the secret number in " + num_guess.to_s() + "
tries!"

end

# Variables ####################
play_game = "yes"
magic_number = 0
num_guesses = 0
num_round = 1
guess = 0

puts "Welcome to the guessing game!\n"
name = get_user_name
puts "Hi, " + name + "!"
puts "You job is to guess the number between 1 and 100"
puts "I will tell you if your guess is correct, too low or too high."

while (play_game.downcase() == "yes") do
puts "Time for Round " + num_round.to_s() + "!"
magic_number = rand(100) + 1
puts magic_number

puts get_user_guess(magic_number)

puts "Would you like to play again?"
play_game = gets().chomp()

num_round = num_round + 1
end

puts "\nThank you for playing the Ruby Guessing Game, " + name + "!
Good-bye!"
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]
using custom methods. I got it to work...but after some lines it'll put
"nil" on the next line and continue with the program.

ex. after an input

Please enter a number.
4
nil
Too low. Try again.
Please enter a number.
7
nil
Too low. Try again.

or after a generated line

You guessed the secret number in X tries!
nil

Why is it saying "nil" everywhere and how do I stop it?

My code is below if that helps...
Your code works correctly, it just prints nil because of the following:

def num_eval(guess, number)
if (guess > 100)
puts "That's more than 100! Pick a number between 1 and 100!"
elsif (guess < 1)
puts "That's less than 1! Pick a number between 1 and 100!"
elsif (guess > number)
puts "Too high! Try again!"
elsif (guess < number)
puts "Too low! Try again!"
end
end
That is how your num_eval function is declared. Each of those if statements
prints to the screen, but the print statement itself returns nil. And the if
statement returns whatever the last line in it's scope returns, so the if
statement returns the nil that the print statement returns. And the last
returned value in the function is what the function returns. So the if
statement returns nil, and that goes out of the function as the return
value.

Then, where you actually use the code below:

guess = gets().chomp().to_i()
puts num_eval(guess, number)

You can see that you print the value that num_eval returns, which is nil, as
described above.

So, your program works (at least for my 2 trials), but it just prints nil
because num_eval returns nil, and you are printing it's value.
 
R

Rick DeNatale

Your code works correctly, it just prints nil because of the following:

def num_eval(guess, number)
That is how your num_eval function is declared. Each of those if statemen= ts
prints to the screen, but the print statement itself returns nil. And the= if
statement returns whatever the last line in it's scope returns, so the if
statement returns the nil that the print statement returns. And the last
returned value in the function is what the function returns. So the if
statement returns nil, and that goes out of the function as the return
value.

Then, where you actually use the code below:

=A0 guess =3D gets().chomp().to_i()

You can see that you print the value that num_eval returns, which is nil,= as
described above.

So, your program works (at least for my 2 trials), but it just prints nil
because num_eval returns nil, and you are printing it's value.

And the solution is either to

1) not puts the result of nil
or
2) which I prefer, remove all the puts'eses from num_eval so that it
returns the evaluation as a string:

def num_eval(guess, number)
if (guess > 100)
"That's more than 100! Pick a number between 1 and 100!"
elsif (guess < 1)
"That's less than 1! Pick a number between 1 and 100!"
elsif (guess > number)
"Too high! Try again!"
elsif (guess < number)
"Too low! Try again!"
end
end


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top