Reputation: 1414
I'm testing my basics of Ruby by writing a simple ChingChongCha program. One of my methods takes the choice entered and turns it into a number (for easy of use for future in the program) however this if statement keeps defaulting to the 'else' condition, even though I can see clearly that if rock is entered it matches the if condition perfectly with ROCK. Any ideas?
def user_choice(choice)
# 1 is rock
# 2 is paper
# 3 is scissors
userintchoice = 0
choice.upcase!
# turns user's choice into an integer
puts choice #debugging
if (choice == 'ROCK') then
userintchoice = 1
elsif (choice == 'PAPER') then
userintchoice = 2
elsif (choice == 'SCISSORS') then
userintchoice = 3
else
puts "Invalid Choice!"
end
return userintchoice
end
Code calling this method and getting input is:
puts "What would you like to choose (input Rock, Paper or Scissors and <ENTER>)?"
userstringchoice = gets()
userchoice = user_choice(userstringchoice)
Upvotes: 0
Views: 88
Reputation: 4176
It seems you have to call .strip
on userchoice because otherwise the string will contain a trailing \n
.
Upvotes: 3