From: Vicente Bosch Campos Date: 2010-11-10T16:08:07+09:00 Subject: Re: help with code, new to programming Much better with a begin .. end while!! I always forget about those as I don't use them much. Thanks Scott On Nov 10, 2010, at 12:30 AM, Scott Gonyea wrote: > Correction. When I Apple-R'd textmate went into a loop, so I did the (again = gets) and then I realized textmates Run command sucks (and/or my expectations are too high). > > Here's a shorter way to write things, without going Perl on it. > > > print 'I am going to test if you are psychic, press enter to continue...' > gets > > begin > print "I am thinking of a number between 1 and 10, can you guess what it is?\n : " > guess = gets.to_i > puts "As to the question of you being psychic, the answer is: #{guess}" > print 'Play again? [Y/n]: ' > end while(gets !~ /^n/i) > > > > On Nov 9, 2010, at 2:58 PM, Vicente Bosch Campos wrote: > >> >> Hi Steve, >> >> The main loop of the game would be as follows: >> >> print 'I am going to test if you are psychic, press enter to >> continue...' >> initialise = gets >> reply='yes' >> >> while reply=='yes' >> matches = ask 'I am thinking of a number between 1 and 10, can you guess what it is?' >> puts 'As to the question of you being psychic the answer is:' >> puts matches >> puts 'Would you like to play again? (please answer yes or no' >> reply = gets.chomp >> end >> >> puts 'Goodbye and farewell...' >> >> Probably there is a more idiomatic way of doing it and also you should check better for the inputed value from the user ( something to investigate for you in the future). >> >> Regards, >> V. >> >> >> On Nov 9, 2010, at 11:31 PM, Steve Rees wrote: >> >>> print 'I am going to test if you are psychic, press enter to >>> continue...' >>> initialise = gets >>> matches = ask 'I am thinking of a number between 1 and 10, can you guess >>> what it is?' >>> puts 'As to the question of you being psychic the answer is:' >>> puts matches >>> puts 'Would you like to play again? (please answer yes or no' >>> reply = gets.chomp >>> if reply == 'yes' >>> matches = ask 'I am thinking of a number between 1 and 10, can you guess >>> what it is?' >>> else >>> puts 'Goodbye and farewell...' >>> end >> >> > >