def ask question
while true
puts question
reply = gets.chomp.downcase
if (reply != 'yes' || reply != 'no')
puts 'Please answer "yes" or "no".'
elsif reply == ''
break
end
end
end
puts ‘Hello, and thank you for…’
puts
ask “Do you like eating tacos?” # Ignore this return value
ask “Do you like eating burritos?” # And this one
wets_bed = ask ‘Do you wet the bed?’ # Save this return value
ask ‘Do you like eating chimichangas?’
ask ‘Do you like eating sopapillas?’
puts ‘Just a few more questions…’
ask ‘Do you like drinking horchata?’
ask ‘Do you like eating flautas?’
puts
puts ‘DEBRIEFING:’
puts ‘Thank you for…’
puts
puts wets_bed
am I stuck in a loop where no matter whether I enter ‘yes’ or ‘no’ or
anything at all after I’m asked
Hello, and thank you for…
Do you like eating tacos?
I get back
Please answer “yes” or “no”.
Do you like eating tacos?
because I decided to ask if reply is different than ‘yes’ and/or
different than ‘no’? as well as ask if it’s the same as an empty string,
basically saying that this is a situation that will always be true?
I got this program to work by rewriting it
def ask question
while true
puts question
reply = gets.chomp.downcase
if (reply == ‘yes’ || reply == ‘no’)
break
else
puts ‘Please answer “yes” or “no”.’
if reply == ‘’
break
end
end
etc etc…
, but I want to understand what I’d need to do to get it to work with !=
method.
Any insight / help would be appreciated.
Thanks,
Emeka
PS
I know since I didn’t save a value for wets_bed I’m going to get nil
back, but just want to figure out the other stuff first.
On Fri, Dec 9, 2011 at 19:14, Emeka P. [email protected]
wrote (rearranged a bit for illustrative purposes):
I got this program to work by rewriting it
[from]
while true
puts question
reply = gets.chomp.downcase
if (reply != ‘yes’ || reply != ‘no’)
puts ‘Please answer “yes” or “no”.’
elsif reply == ‘’
break
end
end
[to]
end
end
The trick is that || means “or”. The first time, you’re saying “if
the reply isn’t yes, or it isn’t no, ask again, else if it’s blank,
break”. No matter what the reply is, it’s either going to be not yes,
or not no. The second time, you’re saying 'if it IS yes, or it IS no,
break, else ask again (and if it’s blank, break)". If you want to use
!=, what you need there is && (and) rather than || (or).
If it helps, you can actually use the words “and” and “or” instead of
the || and &&. There are some situations in which there are
differences, but in this case it should be OK.
A general technique that might help is “pseudocoding”. Write out
first, what it would look like in plain English (or whatever your
native language is) if you were giving instructions to a person – a
fairly simple-minded person who is excellent at following instructions
exactly as given (no matter how wrong they may be), very quickly.
Read that over, trying to “think” like such a person. Maybe even
simulate it step by step by writing down the values of variables.
When that gives you the results you want, then start translating it
from English to Ruby. That’s how most people start out learning to
program. When you get good at it, then you can skip the English step.
Thanks for the feedback and help! Definitely clarifies this. Didn’t
cover && as of yet in the book so wasn’t aware of using it as an option.
I’ll definitely try pseudocoding, think it could really help me think
through some of this stuff.
Much appreciated.
-Emeka
Dave A. wrote in post #1036048:
On Fri, Dec 9, 2011 at 19:14, Emeka P. [email protected]
wrote (rearranged a bit for illustrative purposes):
I got this program to work by rewriting it
[from]
while true
puts question
reply = gets.chomp.downcase
if (reply != ‘yes’ || reply != ‘no’)
puts ‘Please answer “yes” or “no”.’
elsif reply == ‘’
break
end
end
[to]
end
end
The trick is that || means “or”. The first time, you’re saying “if
the reply isn’t yes, or it isn’t no, ask again, else if it’s blank,
break”. No matter what the reply is, it’s either going to be not yes,
or not no. The second time, you’re saying 'if it IS yes, or it IS no,
break, else ask again (and if it’s blank, break)". If you want to use
!=, what you need there is && (and) rather than || (or).
If it helps, you can actually use the words “and” and “or” instead of
the || and &&. There are some situations in which there are
differences, but in this case it should be OK.
A general technique that might help is “pseudocoding”. Write out
first, what it would look like in plain English (or whatever your
native language is) if you were giving instructions to a person – a
fairly simple-minded person who is excellent at following instructions
exactly as given (no matter how wrong they may be), very quickly.
Read that over, trying to “think” like such a person. Maybe even
simulate it step by step by writing down the values of variables.
When that gives you the results you want, then start translating it
from English to Ruby. That’s how most people start out learning to
program. When you get good at it, then you can skip the English step.
Didn’t cover && as of yet in the book
so wasn’t aware of using it as an option.
Oh! They’re usually covered more or less together.
Also, as you may have figured out, anything you can do with either
one, you can also do with the other. (Though it will often take
sprinkling in some negations too.) When to use which one, is mainly a
matter of making the code clear, though sometimes there may be
efficiency benefits too.
-Dave
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.