0

I am creating a method that makes you solve random math problems. Code is below:

def subtraction()
   puts "Your goal is to solve the math problem."
   # Asks if user is ready
   ready()

   a = rand(0..5)
   b = rand(0..5)
   c = a - b
   puts "what is #{a} - #{b}?"



   prompt; next_move = gets.chomp


     if next_move == c
       puts "Lucky guess!"
       water()       
    elsif next_move != c
      puts "The answer was: #{c}"
      dead("You suck at life")
    else 
      dead("You didn't type anything")
    end
end

I keep trying to run this and I keep getting the elsif option. Even though my variables match when I check with puts statements. I am not moving in the direction I want to. What am I doing wrong?

2
  • What is the error you are getting? Commented Feb 22, 2014 at 1:54
  • No error, it just keeps going to the els if statement. No moving on to the water() method Commented Feb 22, 2014 at 1:55

1 Answer 1

3

Change

 next_move = gets.chomp

to

next_move = gets.chomp.to_i # gets.to_i will work also.

Kernel#gets will give you string, and you need to convert it to appropriate object as per your need, if your work is not with string object like this example. As per the line c = a - b, I am very much sure, you need to change your string object, that you are getting from stdin to an integer object. So you have to use String#to_i.

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

4 Comments

This worked. Thanks! Did it work because when I request gets.chomp it makes it an automatic string? Does #to_i make the input seen as an integer?
Thank you so much for your help. You kick ass. I understand that Kernal#gets returns a string object.
@XchelHernandez, if the anwser satisfies you you might want to check it as right
Sorry - super new and wasn't sure what to do. Thanks for the advice.

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.