Help with code, new to programming

I am new to programming and have been learning Ruby using online
tutorials to write simple programs that I understand.

I have written a short code for a program which asks you to guess a
number and tells you that you are/ are not psychic if you guess it
correctly.
I want to put in a function that allows you to continue playing if you
wish or leave when you want to.

Below is the code I have come up with so far, I am aware it is ugly and
could be far more concise but it is laid out in a way that I can
understand the flow. If you too have read the same tutorials you will
see that I have cut, pasted and ammended from these. Please can anyone
tell me what I need to add to loop the game as I have described above?

def ask question
  goodAnswer = false
  while (not goodAnswer)
    puts question
    number = gets.chomp
    think = rand(11)

    if number.to_i != ().to_i
      goodAnswer = true
      if number.to_i == think.to_i
        answer = true
        puts 'I WAS thinking of the number ' + think.to_s
        puts 'You ARE psychic!'
      else
        answer = false
        puts 'I was actually thinking of the number ' + think.to_s
        puts 'You are NOT psychic!'
      end
    else
      puts 'Please put your answer as a number between 0 and 10'
    end
  end

  answer
end

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end

···

--
Posted via http://www.ruby-forum.com/.

Hi Steve,

The main loop of the game would be as follows:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
reply='yes'

while reply=='yes'
  matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?'
  puts 'As to the question of you being psychic the answer is:'
  puts matches
  puts 'Would you like to play again? (please answer yes or no'
  reply = gets.chomp
end

puts 'Goodbye and farewell...'

Probably there is a more idiomatic way of doing it and also you should check better for the inputed value from the user ( something to investigate for you in the future).

Regards,
V.

···

On Nov 9, 2010, at 11:31 PM, Steve Rees wrote:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end

To put it abstractly: you need to change the looping condition.
Currently you loop until the user guesses the number correctly. Now
there are two ways to look at the proposed change:

1. You want to loop until the user explicitly states that he wants to
quite (e.g. by entering "quit").
2. You want to loop as long as the user explicitly states that he
wants another round ("Do you want more? [y/n]")

It seems option 1 is a smaller change (i.e. you only need to change
how "goodAnswer" is set). Btw, conventionally the variable should be
named "good_answer").

Kind regards

robert

···

On Tue, Nov 9, 2010 at 11:31 PM, Steve Rees <stevoreesimo@hotmail.com> wrote:

I am new to programming and have been learning Ruby using online
tutorials to write simple programs that I understand.

I have written a short code for a program which asks you to guess a
number and tells you that you are/ are not psychic if you guess it
correctly.
I want to put in a function that allows you to continue playing if you
wish or leave when you want to.

Below is the code I have come up with so far, I am aware it is ugly and
could be far more concise but it is laid out in a way that I can
understand the flow. If you too have read the same tutorials you will
see that I have cut, pasted and ammended from these. Please can anyone
tell me what I need to add to loop the game as I have described above?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

tell me what I need to add to loop the game as I have described above?

i don't think you need a loop.

> cat o.rb
#!/usr/bin/env ruby -w
# encoding: utf-8
def psychic_test
   puts "I'm thinking of a number between 1 & 10."
   print "What's your guess? "
   if gets.to_i == rand(10) + 1
     puts "ARE psychic"
   else
     puts "NOT psychic"
   end
   print "Try again? (y/n): "
   gets.chomp.first =~ /y/i ? psychic_test : exit
end

···

On 09 Nov 4:31 PM, Steve Rees wrote:

> ruby -e "load 'o.rb'; psychic_test"

def ask question
   goodAnswer = false
   while (not goodAnswer)
     puts question
     number = gets.chomp
     think = rand(11)

     if number.to_i != ().to_i
       goodAnswer = true
       if number.to_i == think.to_i
         answer = true
         puts 'I WAS thinking of the number ' + think.to_s
         puts 'You ARE psychic!'
       else
         answer = false
         puts 'I was actually thinking of the number ' + think.to_s
         puts 'You are NOT psychic!'
       end
     else
       puts 'Please put your answer as a number between 0 and 10'
     end
   end

   answer
end

initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?'
else
puts 'Goodbye and farewell...'
end

Ooh, I love these kinds of questions :smiley: Here's my entry:

print 'I am going to test if you are psychic, press enter to continue...'
gets

begin
  print "I am thinking of a number between 1 and 10, can you guess what it is?\n : "
  guess = gets.to_i
  puts "As to the question of you being psychic, the answer is: #{guess}"
  print 'Play again? [Y/n]: '
end while (again = gets) !~ /^n/i

Scott

···

On Nov 9, 2010, at 2:58 PM, Vicente Bosch Campos wrote:

Hi Steve,

The main loop of the game would be as follows:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
reply='yes'

while reply=='yes'
  matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?'
  puts 'As to the question of you being psychic the answer is:'
  puts matches
  puts 'Would you like to play again? (please answer yes or no'
  reply = gets.chomp
end

puts 'Goodbye and farewell...'

Probably there is a more idiomatic way of doing it and also you should check better for the inputed value from the user ( something to investigate for you in the future).

Regards,
V.

On Nov 9, 2010, at 11:31 PM, Steve Rees wrote:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end

Correction. When I Apple-R'd textmate went into a loop, so I did the (again = gets) and then I realized textmates Run command sucks (and/or my expectations are too high).

Here's a shorter way to write things, without going Perl on it.

print 'I am going to test if you are psychic, press enter to continue...'
gets

begin
  print "I am thinking of a number between 1 and 10, can you guess what it is?\n : "
  guess = gets.to_i
  puts "As to the question of you being psychic, the answer is: #{guess}"
  print 'Play again? [Y/n]: '
end while(gets !~ /^n/i)

···

On Nov 9, 2010, at 2:58 PM, Vicente Bosch Campos wrote:

Hi Steve,

The main loop of the game would be as follows:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
reply='yes'

while reply=='yes'
  matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?'
  puts 'As to the question of you being psychic the answer is:'
  puts matches
  puts 'Would you like to play again? (please answer yes or no'
  reply = gets.chomp
end

puts 'Goodbye and farewell...'

Probably there is a more idiomatic way of doing it and also you should check better for the inputed value from the user ( something to investigate for you in the future).

Regards,
V.

On Nov 9, 2010, at 11:31 PM, Steve Rees wrote:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end

Ahh - that's why Ruby is so good - not a single instance of the word
'class' in this whole thread

(until I just spoilt it)

···

--
Posted via http://www.ruby-forum.com/.

initialise = gets
reply='yes'

while reply=='yes'
  matches = ask 'I am thinking of a number between 1 and 10, can you
guess what it is?'
  puts 'As to the question of you being psychic the answer is:'
  puts matches
  puts 'Would you like to play again? (please answer yes or no'
  reply = gets.chomp

This makes sense! By setting the first reply as 'yes' you begin the
loop,
it is then up to the user to continue the loop by choosing whether or
not 'reply' is still yes or not.

steve

···

--
Posted via http://www.ruby-forum.com/\.

tell me what I need to add to loop the game as I have described above?

i don't think you need a loop.

Why do you say that? Steve explicitly asked about modifying his
program so it will continue the game until the user chooses to finish.
As far as I can see your program does not do that.

Granted, you could do that without an explicit loop by using recursion
or maybe even "retry". Still, conceptually it's still a loop even if
you use another (inferior in this case because more complicated)
technique to implement it.

cat o.rb

#!/usr/bin/env ruby -w
# encoding: utf-8
def psychic_test
puts "I'm thinking of a number between 1 & 10."
print "What's your guess? "
if gets.to_i == rand(10) + 1
puts "ARE psychic"
else
puts "NOT psychic"
end
print "Try again? (y/n): "
gets.chomp.first =~ /y/i ? psychic_test : exit
end

ruby -e "load 'o.rb'; psychic_test"

You are making things unnecessary complicated here. Instead you could just do

ruby 'o.rb'

by adding a line "psychic_test" at the end of the script - or even
better: get rid of the method completely.

Kind regards

robert

···

On Thu, Nov 11, 2010 at 2:35 AM, john <john.loves.spam.12@gmail.com> wrote:

On 09 Nov 4:31 PM, Steve Rees wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Much better with a begin .. end while!! I always forget about those as I don't use them much. Thanks Scott

···

On Nov 10, 2010, at 12:30 AM, Scott Gonyea wrote:

Correction. When I Apple-R'd textmate went into a loop, so I did the (again = gets) and then I realized textmates Run command sucks (and/or my expectations are too high).

Here's a shorter way to write things, without going Perl on it.

print 'I am going to test if you are psychic, press enter to continue...'
gets

begin
print "I am thinking of a number between 1 and 10, can you guess what it is?\n : "
guess = gets.to_i
puts "As to the question of you being psychic, the answer is: #{guess}"
print 'Play again? [Y/n]: '
end while(gets !~ /^n/i)

On Nov 9, 2010, at 2:58 PM, Vicente Bosch Campos wrote:

Hi Steve,

The main loop of the game would be as follows:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
reply='yes'

while reply=='yes'
  matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?'
  puts 'As to the question of you being psychic the answer is:'
  puts matches
  puts 'Would you like to play again? (please answer yes or no'
  reply = gets.chomp
end

puts 'Goodbye and farewell...'

Probably there is a more idiomatic way of doing it and also you should check better for the inputed value from the user ( something to investigate for you in the future).

Regards,
V.

On Nov 9, 2010, at 11:31 PM, Steve Rees wrote:

print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end

Ahh - that's why Ruby is so good - not a single instance of the word
'class' in this whole thread

Did you mean Ruby - the language or Ruby - the community?

(until I just spoilt it)

Bad boy! As punishment write three classes that implement the OP's problem in a OO way! :-)))

Cheers

  robert

···

On 11/10/2010 06:06 PM, Mike Stephens wrote:

Thank you for the help!
I will look at what works and doesnt work to get this short game working
smoothly

cheers!

steve

···

--
Posted via http://www.ruby-forum.com/.

Robert Klemme wrote in post #960582:

Did you mean Ruby - the language or Ruby - the community?

The community is very classy, of course!

···

--
Posted via http://www.ruby-forum.com/\.