From: john Date: 2010-11-11T10:35:18+09:00 Subject: Re: help with code, new to programming On 09 Nov 4:31 PM, Steve Rees wrote: > tell me what I need to add to loop the game as I have described above? i don't think you need a loop. > cat o.rb #!/usr/bin/env ruby -w # encoding: utf-8 def psychic_test puts "I'm thinking of a number between 1 & 10." print "What's your guess? " if gets.to_i == rand(10) + 1 puts "ARE psychic" else puts "NOT psychic" end print "Try again? (y/n): " gets.chomp.first =~ /y/i ? psychic_test : exit end > ruby -e "load 'o.rb'; psychic_test" > def ask question > goodAnswer = false > while (not goodAnswer) > puts question > number = gets.chomp > think = rand(11) > > if number.to_i != ().to_i > goodAnswer = true > if number.to_i == think.to_i > answer = true > puts 'I WAS thinking of the number ' + think.to_s > puts 'You ARE psychic!' > else > answer = false > puts 'I was actually thinking of the number ' + think.to_s > puts 'You are NOT psychic!' > end > else > puts 'Please put your answer as a number between 0 and 10' > end > end > > answer > end > > 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 >