title: Trying to write a string to a tempfile
body: <p>I'm making a game of 20 questions. In the code I've created a tempfile to keep track of the user's questions. Here's the code:</p>
  
  <pre><code>import random
  import turtle  
  import tempfile  
  import gzip  
  
  def getAnswer():  
      obj = random.randrange(5)  
      if obj == 0:  
          infile1 = open("appleyes.txt", "r")  
          infile2 = open("applecanbe.txt", "r")  
          answer = "apple"  
      elif obj == 1:  
          infile1 = open("dogyes.txt", "r")  
          infile2 = open("dogcanbe.txt", "r")  
          answer = "dog"  
      elif obj == 2:  
          infile1 = open("carrotyes.txt", "r")  
          infile2 = open("carrotcanbe.txt", "r")  
          answer = "carrot"  
      elif obj == 3:  
          infile1 = open("flyyes.txt", "r")  
          infile2 = open("flycanbe.txt", "r")  
          answer = "fly"  
      elif obj == 4:  
          infile1 = open("caryes.txt", "r")  
          infile2 = open("carcanbe.txt", "r")  
          answer = "car"  
      print(answer)  
      return infile1, infile2, answer  
  
  def startAsking(infile1, infile2):  
      count = 1  
      tfile = tempfile.TemporaryFile()  
      while count &lt;= 20:  
          ask = input("Is it/Does it have: ")  
          if ask.isalpha():  
              if ask.lower() in tfile:  
                  print("You've already asked this.\n")  
              else:  
                  with gzip.open(tfile+".gz","wb") as f_out:  
                      f_out.write(bytes(ask, 'UTF-8'))  
                  if ask.lower() in infile1.split():  
                      print("Yes it is/Yes it could\n")  
                      count = count + 1  
                  elif ask.lower() in infile2.split():  
                      print("It can be/It could\n")  
                      count = count + 1  
                  else:  
                      print("No or not sure\n")  
                      count = count + 1  
          else:  
              print("No numbers or symbols please.\n")  
  
      infile1.close()  
      infile2.close()  
      tfile.close()  
  
  def guessingTime(answer):  
      print("That's 20! Time to guess.\n")  
      guess = eval(input("Is it a(n): "))  
      if guess.lower() == answer:  
          print("You got it! Congratulations!\n")  
      else:  
          print("Sorry, but the answer was\n")  
  
  def main():  
      infile1, infile2, answer = getAnswer()  
      startAsking(infile1, infile2)  
      guessingTime(answer)  
  
  main()  `#Brian Reser   H787A975
  #Python Project
  #Program plays 20 questions with the user. It randomly pulls a text file for the answer and keeps track of the user's answers.
  
  import random
  import turtle
  import tempfile
  import gzip
  
  def getAnswer():
      obj = random.randrange(5)
      if obj == 0:
          infile1 = open("appleyes.txt", "r")
          infile2 = open("applecanbe.txt", "r")
          answer = "apple"
      elif obj == 1:
          infile1 = open("dogyes.txt", "r")
          infile2 = open("dogcanbe.txt", "r")
          answer = "dog"
      elif obj == 2:
          infile1 = open("carrotyes.txt", "r")
          infile2 = open("carrotcanbe.txt", "r")
          answer = "carrot"
      elif obj == 3:
          infile1 = open("flyyes.txt", "r")
          infile2 = open("flycanbe.txt", "r")
          answer = "fly"
      elif obj == 4:
          infile1 = open("caryes.txt", "r")
          infile2 = open("carcanbe.txt", "r")
          answer = "car"
      print(answer)
      return infile1, infile2, answer
  
  def startAsking(infile1, infile2):
      count = 1
      tfile = tempfile.TemporaryFile()
      while count &lt;= 20:
          ask = input("Is it/Does it have: ")
          if ask.isalpha():
              if ask.lower() in tfile:
                  print("You've already asked this.\n")
              else:
                  with gzip.open(tfile+".gz","wb") as f_out:
                      f_out.write(bytes(ask, 'UTF-8'))
                  if ask.lower() in infile1.split():
                      print("Yes it is/Yes it could\n")
                      count = count + 1
                  elif ask.lower() in infile2.split():
                      print("It can be/It could\n")
                      count = count + 1
                  else:
                      print("No or not sure\n")
                      count = count + 1
          else:
              print("No numbers or symbols please.\n")
  
      infile1.close()
      infile2.close()
      tfile.close()
  
  def guessingTime(answer):
      print("That's 20! Time to guess.\n")
      guess = eval(input("Is it a(n): "))
      if guess.lower() == answer:
          print("You got it! Congratulations!\n")
      else:
          print("Sorry, but the answer was\n")
  
  def main():
      infile1, infile2, answer = getAnswer()
      startAsking(infile1, infile2)
      guessingTime(answer)
  
  main()
  </code></pre>
  
  <p>The error comes along when it reaches the part where it writes the string "ask" to the tempfile. How do I fix this?  </p>
  
  <blockquote>
    <p>TypeError: unsupported operand type(s) for +: '_TemporaryFileWrapper' and 'str'</p>
  </blockquote>