This code doesn’t function the way I expect. The if part is always
skipped no matter what input put into it.
puts “Please enter ‘the password.’”
x = gets
if x != “the password”
puts “Try again”
puts “That is correct. Now, please enter your name.”
I get the same problem if I put
if x == “the password”
puts “try again”
It doesn’t output “correct” it just skips to the “Try again” and
executes the “next.”
After you use “gets”, add this line:
That will allow you see exactly what the variable x contains. You’ll
find that it has a newline character on the end of the string, so
instead of “the password”, it will be “the password\n”
If you want to remove any trailing spaces, and the character you get
from pressing Enter, use gets.chomp
That is a classical newcomer mistake.
Collin, always make sure that your expectation matches the code.
In this context a simple .chomp would remove the trailing newline.
Your variable contains “the password\n”, which is not equal to “the