Reputation: 1
an example of the code I am trying to execute is as follows:
puts 'Please choose "A" or "B"?'
STDOUT.flush
@answer = gets.chomp.upcase
while @answer != "A" || "B"
puts 'Invalid answer, choose "A" or B"'
STDOUT.flush
@answer = gets.chomp.upcase
end
How can I get this to stop looping once A or B is entered. If I remove the While loop I can get @answer to become A or B but once I add that it continually loops no matter what I type in.
Upvotes: 0
Views: 67
Reputation: 110665
I prefer
@answer = nil
until @answer == 'A' || @answer == 'B' do
@answer = gets.chomp
end
Upvotes: 0
Reputation: 118261
The below way will also work :
unless ["A","B"].include? @answer
Upvotes: 0
Reputation: 621
I think your WHILE condition is not good, you can't just say A || B, maybe repeat the condition? Maybe like this?
while @answer != "A" || @answer != "B"
Hope it helps. :)
(edit: maybe even use AND (&&)
instead of OR (||)
?)
Upvotes: 1