D7m
D7m

Reputation: 11

Ruby script need fix

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])

Upvotes: 0

Views: 102

Answers (1)

Agis
Agis

Reputation: 33636

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.

Upvotes: 1

Related Questions