why doesn't this code work properly? i want the program to taken fifty steps and stop, but it keeps counting

why doesn't this code work properly? i want the program to taken fifty steps and stop, but it keeps counting


You told it to loop forever. It loops forever! Telling it to print "Stop." doesn't make it stop. It makes it print "Stop.", on the 50'th time through the loop. The ***50.times*** answer is the Ruby way.




Because you don’t break the while loop anywhere. You start off with a while true, which is always true. It’s an infinite loop.


As others have said there are better ways to do this in ruby but your way would be more like this `def start_game` `while @steps_taken <= 50` `puts "you have taken #{@steps_taken} steps."` `@steps_taken +=1` `end` `end`


Use a 50.times loop


Yes, this is the ruby way.


This is the way


This. Your entire program can be expressed as: 50.times {|i| puts "You have taken #{i} steps"}


Add a `break` to your `if @steps_taken == 50` branch, after the `puts`


Do you want `steps_limit == 50` to do an assignment or actually check equality? If it's an assignment, you should use an instance variable (@steps_limit) and the assignment operator (=) if it's an equality check then you should move it out if the initialixer and info the main loop Also --are you sure you want to do `while true` instead of ` while @steps < @steps_limit` or something? Using break from a loop feels like codesmell here


Maybe I'm crazy but a pet peeve of mine: Rule #1. Pictures of code are the root of all evil. Here's a working sample of your code: [http://rubyfiddle.com/riddles/3c180/4](http://rubyfiddle.com/riddles/3c180/4) * You do "while true" and never break the loop. * You create steps\_taken... then don't use it. Most likely because... * initialize: * assign steps\_taken * run game method * assign steps\_limit * when you run the game method, `steps_limit` hasn't been assigned yet. I assume you were getting a "nil" error of some sort because limit wasn't assigned yet. If you use a WHILE, you need to have a control statement. "While true" means the control statement is "true" and it will continue forever. If you want to use while, you should use "while steps taken less than steps limit". Or you could use "loop do ... break... while" or "for x = 1 to 50" or "until condition do ... end" [https://www.geeksforgeeks.org/ruby-loops-for-while-do-while-until/](https://www.geeksforgeeks.org/ruby-loops-for-while-do-while-until/) Lots of variations in the above article.


Am I only one deeply concerned that there's a method call followed by a boolean in `initialize`?


Is this not needlessly cryptic for a beginner? OP: you might want to revisit and better utilise the steps_limit variable and set it correctly with one =, move it before the method, and then in the method use it to compare to @steps_taken along with some of the other suggestions here to stop the infinite loop


I didn't mean it to be. `initialize` is a special method called when new instances of a class are created with `#new`. It is used to set the initial value of variables you want to use in the class. Multiple methods in the class may use these variables. You may want to set instance variables from class variables, changing their scope. It is not for calling methods and the last line returns a boolean which is not assigned to anything and thus has no purpose. If you want to use the method in this class, outside of the class, define something like : `Game1 = Game.new` to create a new instance of the Game class. The variable name can be anything you like. `Game1.start_game` to call the method on that individual instance. Personally, I would replace `while` with `until` and make your `step_limit` value an argument.


Thank you for taking the time to explain that. Things like this may seem obvious, but when you’re starting out they are really useful to have explained.


i've been following this tutorial https://jsrn.gitbooks.io/make-your-first-text-adventure-in-ruby/content/the_game_loop.html


You mean broken code? It'll error as soon as it hits that line since `steps_limit` is not defined anywhere. I imagine OP meant to initialize and assign on that line, but added an extra `=`.


I haven't written in ruby in quite some time, but it might be because you're using `while true`. Can you use `while steps_taken < 50`?


you never break it


You have `puts “Stop.”` inside your 50th step conditional, but that won’t affect script execution. It’ll just print that text to the screen with a newline and keep going. On the next iteration of the while loop, `@steps_taken` will be 51, 52, etc. You have to use a flow control keyword to tell the script to jump out of the loop. I’m this case, `break` is the keyword you want.




`steps_limit` is not used anywhere else, this code just needs to have a `break` statement.




This is depressing


Yeah you need a break statement and after it passes I would refactor to a few lines


Instead of the while true, you can just create a variable like completed = false, and set the variable to be true once you hit 50 steps. And use while !completed… another alternative. Hope that make senss


Add a break after puts "stop".


you didn't break out of the while loop so it's going to keep incramenting the step counter. once it hits your finish condition add a break statement to stop the loop.


4 spaces not 2 spaces... so I’m not going to read anything else and assume lack of attention to convention