On Jul 31, 2011, at 4:41 PM, Marc Chanliau wrote: > Now, I want to make the snippet above interactive and allow the user to > enter a number and get in return the factorial for that number. The > following snippet doesn't work, what am I doing wrong? > > def fact(n) > if n == 0 > 1 > else > n * fact(n-1) > end > end > > puts "enter a positive integer: " > n = gets Problem one, your factorial function is expecting to work with an integer value, but gets returns a string. Instead you can convert it straight from gets: n = gets.to_i > puts "factorial: " + fact(n) > Next the return value of fact is an integer value, but you're using + to concat what is expected to be two strings. Instead you can inline the fact call, which will call .to_s on the result in the background: puts "factorial: #{fact(n)}" Here's the full code: def fact(n) if n == 0 1 else n * fact(n-1) end end puts "enter a positive integer: " n = gets.to_i puts "factorial: #{fact(n)}" Regards, Chris White Twitter: http://www.twitter.com/cwgem