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!"
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!"