I am learning Ruby as my first programming language using the excellent
tutorial by Chris Pine http://pine.fm/LearnToProgram/
Everything was going well until I tried to write a simple program to
print out the 99 bottles of beer on the wall song. Here is my code.
int = 99
while int != 1
puts int.to_s + ' bottles of beer on the wall, ' + int.to_s + '
bottles of beer'
int.to_i - 1
puts 'Take one down, pass it around, '+int.to_s + ' bottles of beer on
the wall'
end
When I run this, I get into a loop of 99. It never subtracts one. I have
figured out all the other programs that you are to try, but for some
reason the problem in this is escaping me. Any help will be greatly
appreciated.
You're not actually storing the result of subtracting 1 from int anywhere,
so as far as Ruby is concerned, it never changes.
You want something like
int = int - 1
instead of
int.to_i - 1
Note that calling to_i on int is not really necessary here, since int
already contains an integer.
Dan
···
On Wed, Apr 04, 2012 at 08:26:23PM +0900, Bryan Bales wrote:
I am learning Ruby as my first programming language using the excellent
tutorial by Chris Pine http://pine.fm/LearnToProgram/
Everything was going well until I tried to write a simple program to
print out the 99 bottles of beer on the wall song. Here is my code.
int = 99
while int != 1
puts int.to_s + ' bottles of beer on the wall, ' + int.to_s + '
bottles of beer'
int.to_i - 1
puts 'Take one down, pass it around, '+int.to_s + ' bottles of beer on
the wall'
end
When I run this, I get into a loop of 99. It never subtracts one. I have
figured out all the other programs that you are to try, but for some
reason the problem in this is escaping me. Any help will be greatly
appreciated.
--
Daniel Bye
_
ASCII ribbon campaign ( )
- against HTML, vCards and X
- proprietary attachments in e-mail / \
like for me int.to_i - 1 does operation but do not handle result
try:
int = int -1
···
On 04/04/2012 02:26 PM, Bryan Bales wrote:
I am learning Ruby as my first programming language using the excellent
tutorial by Chris Pine http://pine.fm/LearnToProgram/
Everything was going well until I tried to write a simple program to
print out the 99 bottles of beer on the wall song. Here is my code.
int = 99
while int != 1
puts int.to_s + ' bottles of beer on the wall, ' + int.to_s + '
bottles of beer'
int.to_i - 1
puts 'Take one down, pass it around, '+int.to_s + ' bottles of beer on
the wall'
end
When I run this, I get into a loop of 99. It never subtracts one. I have
figured out all the other programs that you are to try, but for some
reason the problem in this is escaping me. Any help will be greatly
appreciated.