From: Paul Mckibbin Date: 2010-09-03T02:05:58+09:00 Subject: Re: New to ruby - Need help with simple guessing program Hi Evan, As others have said, you could have a single control loop, and run everything in that. Something like won=false while !won if won=true end end The random number in your code will change every time rand(10) is called, so for the user they won't know if their next guess should be higher or lower. Also having a single place for input will make it easier to do other things for that input. (e.g. count=0 outside the loop and count+=1 inside, rather than multiple count+=1 for every line requiring user input.) For comparison, you can use the <=> operator which will return -1 if lower, 1 if higher and 0 if they are the same. Using this combination of things, it is possible to code the whole game in about 20 clear lines of ruby and then to add 3 lines to make it keep a count and display that count at the end. (16 was what my code example was, others might come up with even fewer.) Since this is practice for you, I've held off on putting any real code here, but as a hint, take a look at using case...when...else if you try with <=>. One good place to go to for practice code is http://rubylearning.com/blog/. They set a coding challenge every month, aimed at new ruby developers. Good luck and enjoy, Paul Evan Riley wrote: > Ok,so as a little practice I've decided to come up with a Guessing Game > where the user is able to guess a number, and if it the same as the > randomly generated number by the computer, he gets a message that says a > little hooray message. That part is done, and completed, but now I need > help > with tracking the amount of entries he makes in gets > > Here is my current code: > > random_number = rand(10) > > puts 'Can you guess the number?' > puts 'Select a number from 1-10 please' > > players_number = gets.chomp > > while players_number.to_i != random_number.to_i > > while players_number.to_i < random_number.to_i > if players_number.to_i < random_number.to_i > puts 'Sorry your to low' > puts '_________________' > random_number = rand(10) > > puts 'Can you guess the number?' > puts 'Select a number from 1-10 please' > > players_number = gets.chomp > end > end > > while players_number.to_i > random_number.to_i > if players_number.to_i > random_number.to_i > puts 'Sorry your to high' > puts '__________________' > random_number = rand(10) > > puts 'Can you guess the number?' > puts 'Select a number from 1-10 please' > > players_number = gets.chomp > end > end > > while players_number.to_i == random_number.to_i > if players_number.to_i == random_number.to_i > puts 'Congratluations! You Win!' > puts '_________________________' > random_number = rand(10) > > puts 'Can you guess the number?' > puts 'Select a number from 1-10 please' > > players_number = gets.chomp > else > > end > end > end > > Can anyone help me with tracking the amount of entries he makes? Thank > you. -- Posted via http://www.ruby-forum.com/.