1

I am trying to get Python 3.5 to look in a file for a variable. I have tried to convertusername to str(username) but to no avail. Here is the offending piece of code:

if str(username) in fi.read():
         hasAccount = True
         print("found username")
      else:
         hasAccount = False
         print("notfound")

Any ideas? This one has me absolutely baffled. Thanks

P.S. I have already used with open("usernames.txt", "a+")as fi: earlier in the code.

P.P.P.S. People were asking for an MCVE, so I hope this counts:

with open("usernames.txt", "a+")as fi:#opens usernames file as "fi" and closes once finished
with open("userinfos.txt", "a+") as f:#opens userinfos file as "f" and closes once finished
   with open("quizzes.txt", "a+") as fil:
      listofinfos = ["surname", "yeargroup"]#list with information apart from name and age

      if True:#will always run
         name = input("Enter your name:")
         age = input("Enter your age:")
         username = name[:3]+age#shortens name variable to first 3 letters and concatenates with age

      if str(username) in fi.read():
         hasAccount = True
         print("found username")
      else:
         hasAccount = False
         print("notfound")
SupaSped
  • 11
  • 3
  • are you calling `fi.read()` several times in your code? do you have more than 1 condition? you can do `fi.read()` only once without rewinding. – Jean-François Fabre Oct 19 '17 at 19:32
  • P.P.S. I have also searched for solutions, but obviously didn't find anything, or I wouldn't be asking! – SupaSped Oct 19 '17 at 19:33
  • 1
    a [mcve] would be good at this point. – Jean-François Fabre Oct 19 '17 at 19:33
  • @Jean-FrançoisFabre Yes I am, how does that affect it? – SupaSped Oct 19 '17 at 19:33
  • Post the minimal file to reproduce your problem (only whatever the username is?), such that if we ran the script above we can see the failure. – kabanus Oct 19 '17 at 19:33
  • A better practice is to save `fi.read()` as a variable because you can only do this once per file open. If you have multiple `read()` to `fi`, the rest will return nothing. – TYZ Oct 19 '17 at 19:34
  • @SupaSped my crystal ball was right. `fi.read()` reads the file. Next call reads nothing because you're at the end of the file – Jean-François Fabre Oct 19 '17 at 19:34
  • Is this OK? `with open("usernames.txt", "a+")as fi: with open("userinfos.txt", "a+") as f: with open("quizzes.txt", "a+") as fil: listofinfos = ["surname", "yeargroup"] if True: name = input("Enter your name:") age = input("Enter your age:") username = name[:3]+age if str(username) in fi.read(): hasAccount = True print("found username") else: hasAccount = False print("notfound") – SupaSped Oct 19 '17 at 19:36
  • Thanks for all the help, and so quickly! @Jean-FrançoisFabre Should I make a variable with `fi.read` and call that repeatedly, instead of what I did? – SupaSped Oct 19 '17 at 19:37
  • yeah, that's exactly what you should do. – Jean-François Fabre Oct 19 '17 at 19:38
  • Thanks! Is there any way I can credit an answer via comment here? – SupaSped Oct 19 '17 at 19:39

0 Answers0