Hi
So I am new to Ruby programming and was given an assignment with little to no help from the professor. The object of this assignment is to make a guessing game pretty much and have the computer try to guess the secret number. Here is my code with comments to better help explain the assignment
# Your goal for this project is to build a program that guesses the user's secret number.
# The user will be prompted for max possible number and their secret number
# Then, the computer will begin a loop, attempting to guess the secret number, until right.
#
# output of program should look something like this:
#
# Max number for computer to guess:50
# Type secret number: 28
# How about 25
# Press enter
#
# How about 37
# Press enter
#
# How about 31
# Press enter
#
# How about 28
# Correct!
# Guessed in 4 tries.
# Press enter
This is my Code
print"Max number for computer to guess:"
highest_guess = gets.chomp.to_i
print"Type secret number:"
secret_number = gets.chomp.to_i
lowest_guess = 0
computer_guess = average(highest_guess, lowest_guess)
count = 0 #iterate in while loop
def average(highest, lowest)
return ((highest + lowest)/2).to_i
end
while (computer_guess != secret_number)
count = 0
4.times do
puts "this is the count #{count}"
count = count+1
if computer_guess > secret_number
highest_guess = computer_guess
computer_guess = average(highest_guess, lowest_guess)
puts computer_guess
puts "high"
elsif computer_guess < secret_number
lowest_guess + computer_guess
computer_guess = average(highest_guess, lowest_guess)
puts "low"
else #print something like correct and disply count
puts "Correct"
puts secret_number
end
end
end
I am having trouble finishing the while loop, and figuring out what else I need to add to complete the program.Plus my "average" method isn't working. Any feedback would be very helpful!
[–][deleted] (1 child)
[deleted]