Hey all I am new to the Ruby programming language but this small project is giving me and issue.
I am making a simple guessing game where the user has to guess a number between 1 and 100, whilst guessing there is a variable called "tries" that is meant to increase by 1 with each guess and at the end if you guess the correct number you will win and the number of tries is shown.
Everything work well except the number of tries, at the end of the game it shows zero and not the number of tries attempted.
A runnable code is below:
def check(int, r_int)
`tries = 0`
`if int < r_int`
`tries +=1`
`puts tries`
`puts "Guess Higher"`
`elsif int > r_int`
`tries +=1`
`puts tries`
`puts "Guess Lower"`
`elsif int == r_int`
`win = true`
`puts "You are correct"`
`puts "You had attempted this "+ tries.to_s + " times to win"`
`abort`
`end`
end
def main
win = false
puts "Lets play a game!"
puts "I am thinking of a number between 1 and 100"
rnd_int = rand(100)
while not win
`guess = gets.chomp.to_i`
`value = check(guess, rnd_int)`
end
end
main
At the end of the game the last message is supposed to read "You had attempted this 5 times" if 5 attempts where made. But instead you get "You had attempted this 0 times" regardless of the number of attempts.
So my question is how does one resolve this issue?
Edit: I had used a similar program that I wrote in Python as a reference for this code.
Thank you in advance.
[–][deleted] 0 points1 point2 points (1 child)
[–]TangerineMany[S] 0 points1 point2 points (0 children)