Invalid literal for int() with base 10: ' ', reading off text file
<p>Im trying to make a text game/rpg in python. Im getting an</p>

<blockquote>
  <p>line 89, in 
      surv = int(statload.readline(3))</p>
  
  <p>ValueError: invalid literal for int() with base 10: ''</p>
</blockquote>

<p>error code, when trying to read off a file. the other ones around it read fine.</p>

<p>Reading code-</p>

<pre><code>statload = open("Statsheet.txt","r")  
luck = int(statload.readline(2))
surv = int(statload.readline(3))
statload.close
</code></pre>

<p>Code which Writes to file-</p>

<pre><code>stats = open("Statsheet.txt","w")
stats.write(repr(luck)+ "\n")
stats.write(repr(surv)+ "\n")
stats.close
</code></pre>

<p>Contents of Text File-</p>

<pre><code>45
40
</code></pre>

<p>I have to have the "luck" and "surv" stats in "int" format, as later on in the code they are used in mathematical functions. The modules i have imported are "sys", "time", "random", and "math", if that helps at all.</p>

<p>Edit- will put variables into a JSON file instead, as one user suggested, and now know that the "readline" reads the bit value. thanks!</p>

<p>You are using raedline incorrectly. The argument in readline() should be number of bytes to read from the file, not the line number to read.</p>

<p>What you want to be doing is something like this:</p>

<pre><code>with open("Statsheet.txt", "r") as file:
    stats = file.readlines()

luck = int(stats[0])
surv = int(stats[1])
</code></pre>

<p>But there are better options for storing stats etc than text files with each line meaning something, like SQL, json etc.</p>

python sys