From: John Feminella Date: 2011-05-05T17:10:18+09:00 Subject: Re: Lets play a guessing game. (how to code this better?) There's a small bug: ==== puts "Choose a number between 0 and 100" random = rand(100).to_i ==== This actually produces numbers between 0 and 99 inclusive, not 0 and 100. Also the name "random" doesn't describe the point of the variable in the program, which is to serve as the answer. So it might be better to call it "answer", for instance. Finally, `rand(100)` already produces an integer, so you don't need the .to_i method. Also, for this condition, ==== until choice <= 100 and choice >= 0 ==== a more succinct way of writing this is to check if it's included in the range 0..100: ==== until (0..100).include? choice ==== Next, notice that "100" appears a lot, so it might be better to describe what that number is -- it's the maximum value that your random number can take on. So, w emight write: ==== max = 100 ==== and then use `max` everywhere instead of the `100` literal. Otherwise, looks good overall for your first attempt at this, minus a few rough spots. For another challenge, you might try writing a program that can guess a number between 0 and 100 in as few guesses as possible (hint: it should take around 7 guesses). ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Thu, May 5, 2011 at 02:45, Super Goat wrote: > I am a new Rubyist. I told my friend that I was learning Ruby. He asked > me how that was going and then gave me a little challenge. His > challenge, "Write a text game that guesses numbers 0-100". My reply, > "you mean it picks a number at random, and you guess the number, it > tells you higher or lower until you get it?". Up to this point I had not > coded anything in Ruby on my own (aside from the examples in the book) > and I saw this as a great first challenge. So here is what I got. It > took me some time. I ran into trouble because I forgot to take into > consideration the Class of the variables and couldn't figure out why the > loops weren't working. > My question to you all... how could I have done this better or do you > seeing anything that is wrong.  Attached is the code. Thanks for the > feedback. > > Attachments: > http://www.ruby-forum.com/attachment/6168/100guess.rb > > > -- > Posted via http://www.ruby-forum.com/. > >