0

I have been trying to make a while loop in ruby that responds to user input. What is supposed to happen is that when a user inputs the word "Omega" correctly it ends the loop and displays a message, if the word "hint" is entered a message is displayed and the loop repeats, and anything else will have a "try again" message displayed. What is happening is that regardless of what is entered, the loop just asks the original question.

Can anyone find what is wrong with my code, thank you

@GateOneLocked = true
GateOnePassword = String.new('Omega')
GateOneHint= String.new('hint')
#Omega is supposed to be the correct password
while (@GateOneLocked == true) do
puts 'What is the password?'
passwordEntered = gets.to_s
    if (@passwordEntered == @GateOnePassword)
        @GateOneLocked == false
    else if (@passwordEntered != @GateOneHint)
    puts "This is a hint: the password is 'Omega'"
    puts " "
    puts " "
    else
        puts "wrong password, try again"
        puts " "
        puts " "
    end
    end
end
puts 'You entered the correct password!'

2 Answers 2

1

instead @GateOneLocked == false should be @GateOneLocked = false

addiotional remarks:

  • in ruby variable names follow different conventions than C#/Java, instead of GateOneLocked devs write gate_one_locked

  • no need to write String.new, variable = "your_string" is enough (GateOnePassword = 'Omega')

  • while(@GateOneLocked) is enough - no need to check whether it's == true

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

2 Comments

Additionally, gets.to_s will retain a newline on the captured input, using gets.chomp instead will remove it; gets will already return a String. Otherwise, user input will never match the password. You also set GateOneHint as a local variable, but in your comparison call it as an instance variable (@GateOneHint) which without defining will return nil.
@djaszczurowski I took off one of the equal signs, and what happens now is any input displays the "you entered the correct password" message.
0

I made several fixes to your code. I changed the else if to elsif and added @ to the variable references that were missing it so that your code is consistent. (I'd actually ditch them all unless you're using this code in a class.) I changed your @GateOneLocked == false to use the assignment operator instead of the comparison operator. Most importantly, perhaps, I added a chomp call that will remove the \n (new line character) from your user's input. Also, I changed the comparison in the elsif to == so that your user can request a hint, which is what I think you intended.

@GateOneLocked = true
@GateOnePassword = String.new('Omega')
@GateOneHint= String.new('hint')
#Omega is supposed to be the correct password
while (@GateOneLocked == true) do
    puts 'What is the password?'
    @passwordEntered = gets.chomp.to_s
    if (@passwordEntered == @GateOnePassword)
        puts "abc"
        @GateOneLocked = false
    elsif (@passwordEntered == @GateOneHint)
        puts "This is a hint: the password is 'Omega'"
        puts " "
        puts " "
    else
        puts "wrong password, try again"
        puts " "
        puts " "
    end
end
puts 'You entered the correct password!'

Comments

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.