title: How do I get an "if else" loop in r to save values created within the loop and use them in future runs of the loop?
body: <p>I am trying to run a simple loop that will count my points. You can see the code is very simple. I think there's something I am missing here about saving the values within loops to objects. Most information I find is for more complex loops. Feels like this should be a simple solution that I just don't understand yet.</p>
  <pre><code>  test &lt;- function(){
  
    number &lt;- 2
  
    loop &lt;- function(){
  
    entry &lt;- as.numeric(readline(prompt = &quot;Enter Number:&quot;))
  
      if(entry == number){
        points &lt;- 2 * points
        cat(&quot;You have &quot; ,points, &quot; points!&quot;, sep =&quot;&quot;)
        print(&quot;Double points!&quot;)
        
      }else{
        print(&quot;Lose a point.&quot;)
        points &lt;- points - 1
        cat(&quot;You have &quot; ,points, &quot; points!&quot;, sep =&quot;&quot;)
        if(points &gt; (0)){
          loop()
        }else{
          print(&quot;You lose!&quot;)
        }
       }
      }
  
  
     loop()
     } 
     test()
  </code></pre>
  <p>When this runs, user enters 3 and the points become 9 and the loop begins again. If user enters 3 again it should subtract 1 point from 9 to make 8. But the object points value is 9 and will not go less than that.</p>