title: .dat files and writing a random ascii value and converting that back into its character form
body: <p>I am trying to create a program which creates a .txt or a .dat file (depends on user choice) and then in that file writes a random value which is then converted into another value.</p>
  
  <p>so if a random character is given this should be converted into its corresponding ASCII value and vie versa.</p>
  
  <p>At the moment this is what I have:</p>
  
  <pre><code>import random # import randomdeals with random generation
  import string # import string contains sequences of common ASCII characters
  
  userInput = input("Which file would you like to open either a .txt file or a .dat file?") # let the user decide which file type they want to open
  
  def randomCharacter(): 
      return random.choice(string.ascii_letters)
  def randomASCII():
      return random.randint(97,122)
  
  if userInput == (".txt"):
      with open("characterConversion.txt", "w") as textFile:
          textFile.write(randomCharacter())
          textFile.close()
      textFile = open("characterConversion.txt", "r+")
      character = textFile.readline(1)
      textFile.write("\n converted into its ASCII value is \n") #/n means a new line
      textFile.write(str(ord(character)))
      textFile.close()
  if userInput == (".dat"):
      with open("asciiConversion.dat", "w") as datFile:
          datFile.write(str(randomASCII()))
          datFile.close()
      datFile = open("asciiConversion.dat", "r+")
      ascii = int(datFile.readline(1))
      character = chr(randomASCII())
      datFile.write(str(ascii))
      datFile.write("\n in its character form is \n")
      datFile.write(character)
      datFile.close()
  </code></pre>
  
  <p>My .txt part of the program works great thanks to users earlier who helped me fix it but I am now having trouble with my .dat file. After I enter my input as .dat and open the file it reads: 
  1061
   in its character form is 
  a</p>
  
  <p>However the number (in this case it is 1061) should only be between 97 and 122 so I do not know where I am going wrong. I apologise if I haven't explained my problem clearly </p>