0

I'm having a problem with my ruby script. If anyone could help, I'd really appreciate it. The problem is that the number is stuck between 1-2; where 2 is too high and 1 is too low. The guesses should be integers only.

#!/usr/bin/ruby
def highLow(max)
again = "yes"
while again == "yes"
puts "Welcome to the High Low game"
            playGame(max)
            print "Would you like to play again? (yes/no): "
            again = STDIN.gets.chomp

            if again == 'no'
               puts "Have a nice day, Goodbye"

            end
     end
end
#This method contains the logic for a single game and call the feedback method.
def playGame(max)
puts "The game gets played now"


puts "I am thinking of a number between 1 and #{max}." #It show what chosen by user
randomNumber = rand(max)+ 1
print "Make your guess: "
guess = STDIN.gets.chomp

feedback(guess, randomNumber)
end 
#Start while loop
#Logic for feedback method. It's ganna check the guess if it's high or low.
def feedback(guess, randomNumber)
count = 1
while guess.to_i != randomNumber
    count = count + 1
    if guess.to_i < randomNumber
        print "That's too low. Guess again: "
    else
        print "That's too high. Guess again: "
    end
    guess = STDIN.gets.chomp
end
puts "Correct! You guessed the answer in #{count} tries!"

end

highLow(ARGV[0])
1
  • I changed it. Now I get 4 errors. line 38 line 29 line 8 line 49 Commented Feb 19, 2014 at 7:59

1 Answer 1

1

Change your last line to this:

highLow(ARGV[0].to_i)

The ARGV array contains all the passed in arguments as strings, so you have to cast it to integer.

Sign up to request clarification or add additional context in comments.

1 Comment

I still have the same problem !! when I guess 1 it's too low and 2 is too high!! do I have to fix the ARVG in the last line only? or should I put it in another line? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.