r/learnruby • • Mar 25 '14
Simple odd or even test not working?
I'm trying to code an odd or even test and use the modulus operator. It's not working at all. I don't know why but it's only returning "That's not even" for pretty much everything. Any ideas?
def numbertest()
print "Input your number here."
number1=gets.chomp
print "Okay, let's see if #{number1} 's odd or even."
if number1.even?
print "That's even."
else
print "That's not even."
end
end
numbertest()
3
Upvotes
View all comments
Show parent comment
0
u/[deleted] Mar 25 '14
I didn't, but now I am getting the error when I reran the original code. Strange. I'm running it in terminal as a module, if that makes a difference.
Anyway, I made changes to the code and fixed it, this seemed to work! So by itself gets.chomp always returns a string? What if I were to do something with the modulus operator, like this? Or would I still have to convert it to an integer.
This code didn't work either so I'm wondering if that's also the reason.