0

I am new to coding and this is the first proper program I have tried to code.

The first round works as I intended it to but the 2nd and 3rd rounds jump to the line "puts "Wrong!" puts "You have reached the maximum number of guesses. You get zero points for this round""

even if I correctly answer the questions. Any tips on how to fix this?

points = 0
puts "Welcome to my game. There are three rounds of questions in this quiz."
puts "You get a point for each question you get right within 3 guesses" 
puts "Please press enter to continue"
gets

puts "Round 1"
puts "I am thinking of a random number between 1 and 100. You have 3 attempts to guess my number correctly"

random_number = rand(1 .. 100)

guess = ""
guess_count = 0
out_of_guesses = false

while guess != random_number and !out_of_guesses
    if guess_count == 0
        puts "Enter your guess:"
        guess = gets.chomp.to_i
        guess_count += 1
      
    elsif guess_count == 1
      if random_number > 50
        puts "Wrong! Try again"
        puts "Hint 1:"
        puts "My number is bigger than 50"
      elsif random_number < 50
        puts "Wrong! Try again"
        puts "Hint 1:"
        puts "My number is smaller than 50"
      end
        puts "Enter your second guess:"
        guess = gets.chomp.to_i
        guess_count += 1
      
    elsif guess_count == 2
      if guess > random_number 
        diff1 = guess - random_number
        puts "Wrong! Try again"
        puts "Hint 2:"
        puts "My number is #{diff1} numbers away from your guess"
      elsif guess < random_number
        diff2 = random_number - guess
        puts "Wrong! Try again"
        puts "Hint 2:"
        puts "My number is #{diff2} numbers away from your guess"
      end
        puts "Enter your final guess:"
        guess = gets.chomp.to_i
        guess_count += 1

    else 
        out_of_guesses = true
    end
end

if out_of_guesses
    puts "Wrong!"
    puts "You have reached the maximum number of guesses. You get zero points for this round."
    puts "The number I was thinking about was #{random_number}"
else
    points += 1
    puts "Congratulations! You get a point!"
    puts "You correctly guessed that the number I was thinking about is #{random_number} and you did it in #{guess_count} guesses."
end

puts "Press enter to continue"
gets

def answer_my_question(question, right_answer)
answer = ""
out_of_guesses = false
guess_count = 0
guess_limit = 3

puts "You get 3 tries to answer this question and there are no hints in this section"
puts question

while answer != right_answer and !out_of_guesses
  if guess_count == 0
    puts "Enter your first guess"
    answer = gets.chomp.downcase
    guess_count += 1
  elsif guess_count < guess_limit
    puts "Wrong! Try again"
    answer = gets.chomp
    guess_count += 1
  else
    out_of_guesses = true
end
end
end

puts "Round 2"
answer_my_question("What is the capital of Spain?", "madrid")

if out_of_guesses
    puts "Wrong!"
    puts "You have reached the maximum number of guesses. You get zero points for this round"
else
    points += 1
    puts "Congratulations! You get a point!"
    puts "You correctly answered the question."
end

puts "Press enter to continue"
gets

puts "Round 3"
answer_my_question("What continent is jamaica in?", "north america")

if out_of_guesses
    puts "Wrong!"
    puts "You have reached the maximum number of guesses. You get zero points for this round"
else
    points += 1
    puts "Congratulations! You get a point!"
    puts "You correctly answered the question."
end

puts "Press enter to continue"
gets

puts "This is the end of the quiz. You ended up with #{points} points"

I put the downcase method in to try and mitigate any case-sensitivity issues but it is not working

Ema
  • 1
  • 1
  • 4
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Mar 21 '23 at 22:15
  • I ran this code in `irb` and it seemed to work as expected. I got a point for each of the three correctly answered questions and at the end it told me that I ended up with 3 points. – steev Mar 21 '23 at 22:43
  • @steev I think it only works if you correctly guess the first number. If not, I don't think it works. I think it might have something to do with the "out_of_guesses" variable. But I don't know what to do – Ema Mar 21 '23 at 23:29
  • I ran it again and intentionally guessed incorrectly on the first attempt. It still behaved as expected. I think you need to provide more details such as steps to reproduce because I've gone through two scenarios where it worked as expected. – steev Mar 21 '23 at 23:53

1 Answers1

1

The issue you're encountering is due to the out_of_guesses variable being declared outside of the answer_my_question function. This causes it to retain its value between function calls, which means that if it is set to true in one function call, it will remain true in the next function call, leading to the problem you're experiencing. To fix this, you should declare the out_of_guesses variable inside the answer_my_question function and return it as a result of the function.

raheebwa
  • 11
  • 2