help with code, new to programming

S

Steve Rees

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
 
V

Vicente Bosch Campos

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 =3D gets
reply=3D'yes'

while reply=3D=3D'yes'
matches =3D 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 =3D 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.
 
S

Scott Gonyea

Ooh, I love these kinds of questions :D 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 =3D gets.to_i
puts "As to the question of you being psychic, the answer is: =
#{guess}"
print 'Play again? [Y/n]: '
end while (again =3D gets) !~ /^n/i

Scott

=20
Hi Steve,
=20
The main loop of the game would be as follows:
=20
print 'I am going to test if you are psychic, press enter to
continue...'
initialise =3D gets
reply=3D'yes'
=20
while reply=3D=3D'yes'
matches =3D 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 =3D gets.chomp
end
=20
puts 'Goodbye and farewell...'
=20
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).
 
S

Scott Gonyea

Correction. When I Apple-R'd textmate went into a loop, so I did the =
(again =3D 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 =3D 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)



=20
Hi Steve,
=20
The main loop of the game would be as follows:
=20
print 'I am going to test if you are psychic, press enter to
continue...'
initialise =3D gets
reply=3D'yes'
=20
while reply=3D=3D'yes'
matches =3D 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 =3D gets.chomp
end
=20
puts 'Goodbye and farewell...'
=20
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).
 
V

Vicente Bosch Campos

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

Correction. When I Apple-R'd textmate went into a loop, so I did the =
(again =3D gets) and then I realized textmates Run command sucks (and/or =
my expectations are too high).
=20
Here's a shorter way to write things, without going Perl on it.
=20
=20
print 'I am going to test if you are psychic, press enter to = continue...'
gets
=20
begin
print "I am thinking of a number between 1 and 10, can you guess what = it is?\n : "
guess =3D 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)
=20
=20
=20
On Nov 9, 2010, at 2:58 PM, Vicente Bosch Campos wrote:
=20
=20
Hi Steve,
=20
The main loop of the game would be as follows:
=20
print 'I am going to test if you are psychic, press enter to
continue...'
initialise =3D gets
reply=3D'yes'
=20
while reply=3D=3D'yes'
matches =3D 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 =3D gets.chomp
end
=20
puts 'Goodbye and farewell...'
=20
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).
 
R

Robert Klemme

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?

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
 
M

Mike Stephens

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)
 
R

Robert Klemme

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
 
S

Steve Rees

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

cheers!

steve
 
S

Steve Rees

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
 
J

john

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

i don't think you need a loop.
#!/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"
 
R

Robert Klemme

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.
#!/usr/bin/env ruby -w
# encoding: utf-8
def psychic_test
=A0puts "I'm thinking of a number between 1 & 10."
=A0print "What's your guess? "
=A0if gets.to_i =3D=3D rand(10) + 1
=A0 =A0puts "ARE psychic"
=A0else
=A0 =A0puts "NOT psychic"
=A0end
=A0print "Try again? (y/n): "
=A0gets.chomp.first =3D~ /y/i ? psychic_test : exit
end

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top