Created
January 29, 2012 22:04
-
-
Save agentfin/1700935 to your computer and use it in GitHub Desktop.
Chris Pine PragProg 9.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First version with a bunch of excess variables | |
# direct from Mr. Pine | |
def ask question | |
good_answer = false | |
while (not good_answer) | |
puts question | |
reply = gets.chomp.downcase | |
if (reply == 'yes' or reply == 'no' ) | |
good_answer = true | |
if reply == 'yes' | |
answer = true | |
else | |
answer = false | |
end | |
else | |
puts 'Please answer "yes" or "no".' | |
end | |
end | |
answer # This is what we return (true or false) | |
end | |
puts 'Hello, and thank you for smoking.' | |
puts | |
ask 'Do you like pina-coladas?' | |
ask 'Do you like getting caught in the rain?' | |
risky_business = ask 'Do you know what your sig other likes?' | |
ask 'Are you into yoga?' | |
ask 'Do you have half a brain?' | |
puts 'Just a few more questions.' | |
ask 'Do like the feel of the ocean?' | |
ask 'Are you into champagne?' | |
puts | |
puts 'DEBRIEFING' | |
puts 'Thank\'s for all the fish' | |
puts | |
puts risky_business |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nice little questionnaire | |
# any suggestions of TESTS that should be in here, greatly appreciated! | |
def ask question | |
reply = false | |
while ( ! reply ) | |
puts question | |
reply = gets.chomp.downcase | |
if (reply == 'yes' || reply == 'no' ) | |
reply = true | |
else | |
reply = false | |
puts 'Please answer "yes" or "no".' | |
end | |
end | |
end | |
puts 'Hello, and thank you for smoking.' | |
puts | |
ask 'Do you like pina-coladas?' | |
ask 'Do you like getting caught in the rain?' | |
risky_business = ask 'Do you know what your sig other likes?' | |
ask 'Are you into yoga?' | |
ask 'Do you have half a brain?' | |
puts 'Just a few more questions.' | |
ask 'Do like the feel of the ocean?' | |
ask 'Are you into champagne?' | |
puts | |
puts 'DEBRIEFING' | |
puts 'Thank\'s for all the fish' | |
puts | |
puts risky_business |
The first one works fine. The challenge is to REMOVE 'answer' and 'good_answer' from the code.
That's where I'm getting locked up, since it seems I need to store or at least check 'reply' with each pass. Otherwise, my 'while' is just going over the same question over and over, rather than moving from one to the next.
Was able to get it to through all the questions, but when the 'yes/no' failed, the "Please answer yes or no" was being counted as a question and then skipping the next one in line.
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps try breaking out some of the chunks in to independent methods? For example:
Add methods that make the code read more like natural language until you feel like you can understand where the problem with the logic might be.