Hi,
Welcome!
Hello i am new to programming/ ruby but i have had some help from a
cousin and read a book so i have some knowlage of what I'm doing.
Anyways in my free time i was creating a program that tested me on easy
multiplication problems to keep my mind fresh;
puts 'How many problems do you want to solve?'
problems = gets.chomp
5.downto(1) do |x|
puts "The test will beging #{x} in seconds! "
sleep 1
end
puts 'Start!'
def multiply
multiple2 = rand(11)
multiple1 = rand(11S)
answer = multiple1 * multiple2
puts 'what is ' + multiple1.to_s + ' * ' + multiple2.to_s + ' ?'
answerp = gets.chomp
if answer.to_s == answerp.to_s
puts "good job"
else puts "You fail!"
end
end
problems.to_i.times do multiply
end
:
So i was wondering, how can i tally up how many times they answer
correctly. I tried putting in the definition after the if statement
correct + 1, * obviously i made the variable "correct" above* but the
problem is, i cant retrive the variable out of the definition i had
made. which brings me to my question of instance variables. Can't i make
a seperate def and create and instance variable like
def correct
@correct = correct
then in my multiplication def. under the if statement do
correct + 1
then finally at the end do
puts correct
and that would give me the total correct, but thats not working for me?
i dont know what im doing wrong? I see these instance variable in this
confusing book i just started to read, and it looks like maybe there
needs to be a class?!?!? idk but if someone could help me get this stuff
figured out that would be awesome!
As you say, an instance variable would work, this is written as: @correct -- I've inserted these below (unless I edited this incorrectly, it should work).
puts 'How many problems do you want to solve?'
problems = gets.chomp
@correct = 0
5.downto(1) do |x|
puts "The test will beging #{x} in seconds! "
sleep 1
end
puts 'Start!'
def multiply
multiple2 = rand(11)
multiple1 = rand(11)
answer = multiple1 * multiple2
puts 'what is ' + multiple1.to_s + ' * ' + multiple2.to_s + ' ?'
answerp = gets.chomp
if answer.to_s == answerp.to_s then
puts "good job"
@correct = @correct + 1
else
puts "You fail!"
end
end
problems.to_i.times do
multiply
end
puts "correct: #{@correct}"
Now you'll be asking yourself: so what is the instance that this is a variable of? And: what's running this program? Good questions. You'll need to learn about classes, objects, and scopes to answer them.
There is an alternative technique using parameters and returned values:
puts 'How many problems do you want to solve?'
problems = gets.chomp
5.downto(1) do |x|
puts "The test will beging #{x} in seconds! "
sleep 1
end
puts 'Start!'
def multiply(correct)
multiple2 = rand(11)
multiple1 = rand(11)
answer = multiple1 * multiple2
puts 'what is ' + multiple1.to_s + ' * ' + multiple2.to_s + ' ?'
answerp = gets.chomp
if answer.to_s == answerp.to_s then
puts "good job"
correct = correct + 1
else
puts "You fail!"
end
return correct
end
correct = 0
problems.to_i.times do
correct = multiply(correct)
end
puts "correct: #{correct}"
Another thing I should mention, since you are just starting out... it is best if you are *very* rigourous about your code from the very beginning. This includes things like formatting your code. Some habits are hard to un-learn, so don't form them 
Have fun!
Cheers,
Bob
···
On 1-Oct-07, at 10:21 PM, Erik Boling wrote:
ps: sorry long post 
--
Posted via http://www.ruby-forum.com/\.
----
Bob Hutchison -- tumblelog at http://www.recursive.ca/so/
Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch
http://www.recursive.ca/ -- works on http://www.raconteur.info/cms-for-static-content/home/