I realize this is an extremely noobish question, but i'm stuck and can't
seem to figure it out (aside from some very simple HTML, i know nothing
about programming).
I'm trying this question he suggested:
• Write a program which asks for a person's favorite number. Have your
program add one to the number, then suggest the result as a bigger and
better favorite number. (Do be tactful about it, though.)
This is what i have, but it's not working:
puts 'What is your favorite number?'
number = gets
puts 'That is a good number, but ' + (number.to_i + 1) + ' is better!'
However, if i remove the (number.to_i + 1) from that line, it prints out
the text just fine. And if i remove the text, so the line is only:
puts number.to_i + 1
I get the correct results. But if i try to combine the two, i get:
TypeError: cannot convert Fixnum into String
method + in untitled document at line 3
at top level in untitled document at line 3
You're trying to merge a String and a Fixnum into one object (i.e.,
you feed one big string obect to puts; you can't merge a Fixnum and
String like that). The to_s method will convert the Fixnum you
created with to_i and added one to back to a string for output.
Kind of confusing I know, but if you got this far, I'm sure youll get it.
--Jeremy
···
On 1/21/07, Nick Nolan <hardcoredummy@hotmail.com> wrote:
I realize this is an extremely noobish question, but i'm stuck and can't
seem to figure it out (aside from some very simple HTML, i know nothing
about programming).
I'm trying this question he suggested:
• Write a program which asks for a person's favorite number. Have your
program add one to the number, then suggest the result as a bigger and
better favorite number. (Do be tactful about it, though.)
This is what i have, but it's not working:
puts 'What is your favorite number?'
number = gets
puts 'That is a good number, but ' + (number.to_i + 1) + ' is better!'
However, if i remove the (number.to_i + 1) from that line, it prints out
the text just fine. And if i remove the text, so the line is only:
puts number.to_i + 1
I get the correct results. But if i try to combine the two, i get:
TypeError: cannot convert Fixnum into String
method + in untitled document at line 3
at top level in untitled document at line 3
puts "What's your favourite number?"
number= gets.to_i
puts "Hmm... not bad, but #{number+1} is better"
···
I realize this is an extremely noobish question, but i'm stuck and can't
seem to figure it out (aside from some very simple HTML, i know nothing
about programming).
I'm trying this question he suggested:
• Write a program which asks for a person's favorite number. Have your
program add one to the number, then suggest the result as a bigger and
better favorite number. (Do be tactful about it, though.)
This is what i have, but it's not working:
puts 'What is your favorite number?'
number = gets
puts 'That is a good number, but ' + (number.to_i + 1) + ' is better!'
However, if i remove the (number.to_i + 1) from that line, it prints out
the text just fine. And if i remove the text, so the line is only:
puts number.to_i + 1
I get the correct results. But if i try to combine the two, i get:
TypeError: cannot convert Fixnum into String
method + in untitled document at line 3
at top level in untitled document at line 3
I realize this is an extremely noobish question, but i'm stuck and can't
seem to figure it out (aside from some very simple HTML, i know nothing
about programming).
I'm trying this question he suggested:
· Write a program which asks for a person's favorite number. Have your
program add one to the number, then suggest the result as a bigger and
better favorite number. (Do be tactful about it, though.)
This is what i have, but it's not working:
puts 'What is your favorite number?'
number = gets
puts 'That is a good number, but ' + (number.to_i + 1) + ' is better!'
However, if i remove the (number.to_i + 1) from that line, it prints out
the text just fine. And if i remove the text, so the line is only:
puts number.to_i + 1
I get the correct results. But if i try to combine the two, i get:
TypeError: cannot convert Fixnum into String
printf "That is good, but %d is better.\n", number.to_i+1
So, there is an ugly "\n" (carriage return) at the end. Let's remove that!
number = gets.chop
p number
When you want to remove a line ending, prefer chomp() to chop(). chomp() will only target the line ending, which will cause you less trouble when using it on a line that doesn't actually have one: