From: Alex Gutteridge Date: 2007-10-02T11:46:18+09:00 Subject: Re: instance variable/ I'm lost On 2 Oct 2007, at 11:21, Erik Boling wrote: > 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. Instance variables are for (as you note below) classes. You don't have a class so you don't need to use instance variables. The simplest solution here would be to use a global ($correct), though globals are usually frowned upon in larger programs: 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 puts "good job" $correct += 1 else puts "You fail!" end end problems.to_i.times do multiply end puts "You scored #{$correct}/#{problems}" > 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! > ps: sorry long post :( I don't know what confusing book you are reading, but their are many excellent tutorials for Ruby which will explain these concepts better than I can. See any of the recent 'I'm a beginner what book should I read?' threads. Alex Gutteridge Bioinformatics Center Kyoto University