B
Benjamin Thomas
Hi, I'm new to Ruby and programming in general.
I'm coding a little script to train on multiplication tables but
something's wrong:
If I type 'exit', the function should normally exit, via a return
statement. But this does not always happen and I really can't understand
why. I've been scratching my head for a few hours on that so I'm posting
here hoping somebody will be able to spot my obvious mistake!
I'm trying to apply here the concept of recursion so maybe this is where
I'm messing up.
Thanks for your input,
Ben
#########################################################################
#!/usr/bin/ruby
require 'yaml'
system "clear"
filename = "tested_list.txt"
if File.exist?(filename)
imported_list = File.read filename
$tested = YAML::load imported_list
else
$tested = {}
end
def rand_multi()
num1 = rand(10)
num2 = rand(10)
got_wrong = false
operation = "#{num1}*#{num2}"
if $tested[operation].nil?
puts "creation!"
$tested[operation] = 0
elsif $tested[operation] > 1
puts "need restart"
rand_multi() # forces function call again
end
puts "iteration #{$tested[operation]}"
print operation, " = "
before = Time.now
time_before = (before.min * 60) + before.sec # in case I go for a
coffee
answer = gets.chomp
if answer =~ /exit/
puts "will exit now"
return ##### <-- return statement does not always exit method; why??
######
# exit
elsif answer =~ /[0-9]+/
answer = answer.to_i
else
puts "Please enter numbers only!"
rand_multi()
end
if answer != num1 * num2
print "[0;31m" # terminal colors
puts "inside not equal"
puts "No! Answer was '#{num1*num2}'"
print "[0;m"
sleep(1)
# system "clear"
got_wrong = true
$tested[operation] -= 5 # give negative value
rand_multi()
elsif answer == num1 * num2
after = Time.now
time_after = (after.min * 60) + after.sec
answer_time = time_after - time_before
print "[1;34m"
puts "inside equal"
puts "Yes!, you answered in #{answer_time} seconds"
print "[0;m"
sleep(0.3)
# system "clear"
if got_wrong == false
if answer_time < 3
$tested[operation] += 1 # give positive value
else
$tested[operation] -= 1 # small penalty if answer too
slow
end
end
rand_multi()
end
end
puts "Multiplications!"
puts "Type 'exit' to exit"
puts "\n\n"
rand_multi()
puts "Bad answers:"
$tested.each_pair {|key, value| puts "#{key} => #{value}" if value < 0}
File.open filename, 'w' do |f|
f.write $tested.to_yaml
end
###############################################
I'm coding a little script to train on multiplication tables but
something's wrong:
If I type 'exit', the function should normally exit, via a return
statement. But this does not always happen and I really can't understand
why. I've been scratching my head for a few hours on that so I'm posting
here hoping somebody will be able to spot my obvious mistake!
I'm trying to apply here the concept of recursion so maybe this is where
I'm messing up.
Thanks for your input,
Ben
#########################################################################
#!/usr/bin/ruby
require 'yaml'
system "clear"
filename = "tested_list.txt"
if File.exist?(filename)
imported_list = File.read filename
$tested = YAML::load imported_list
else
$tested = {}
end
def rand_multi()
num1 = rand(10)
num2 = rand(10)
got_wrong = false
operation = "#{num1}*#{num2}"
if $tested[operation].nil?
puts "creation!"
$tested[operation] = 0
elsif $tested[operation] > 1
puts "need restart"
rand_multi() # forces function call again
end
puts "iteration #{$tested[operation]}"
print operation, " = "
before = Time.now
time_before = (before.min * 60) + before.sec # in case I go for a
coffee
answer = gets.chomp
if answer =~ /exit/
puts "will exit now"
return ##### <-- return statement does not always exit method; why??
######
# exit
elsif answer =~ /[0-9]+/
answer = answer.to_i
else
puts "Please enter numbers only!"
rand_multi()
end
if answer != num1 * num2
print "[0;31m" # terminal colors
puts "inside not equal"
puts "No! Answer was '#{num1*num2}'"
print "[0;m"
sleep(1)
# system "clear"
got_wrong = true
$tested[operation] -= 5 # give negative value
rand_multi()
elsif answer == num1 * num2
after = Time.now
time_after = (after.min * 60) + after.sec
answer_time = time_after - time_before
print "[1;34m"
puts "inside equal"
puts "Yes!, you answered in #{answer_time} seconds"
print "[0;m"
sleep(0.3)
# system "clear"
if got_wrong == false
if answer_time < 3
$tested[operation] += 1 # give positive value
else
$tested[operation] -= 1 # small penalty if answer too
slow
end
end
rand_multi()
end
end
puts "Multiplications!"
puts "Type 'exit' to exit"
puts "\n\n"
rand_multi()
puts "Bad answers:"
$tested.each_pair {|key, value| puts "#{key} => #{value}" if value < 0}
File.open filename, 'w' do |f|
f.write $tested.to_yaml
end
###############################################