0

I would like to take in the variable "name" the input from the user, then I would like load the account.zk (that is a normal text file) in to a list, and then I would like check if the input is already on this list. If is not, I would like to add it, and on the contrary, I would like to skip and go ahead.

I've coded this function myself, but I didn't succeed! Can someone understand why?

# Start Downloading System
name = input(Fore.WHITE+"Enter Profile Name To Download: ")

# Save Account (used for check the updates!)
file_object = open("account.zk", "r")

for line in file_object:
  stripped_line = line.strip()
  line_list = stripped_line.split()
  list_of_lists.append(line_list)


if (len(list_of_lists) != len(set(name))):
    print("\n\nAlready in List!\n\n")
    file_object.close
    time.sleep(5)
else:
    file_object.close
    file_object = open("account.zk", "w")
    file_object.write(name + "\n")
    file_object.close
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zenek
  • 110
  • 1
  • 4
  • 12
  • 'list_of_list' is obviously already declared! – Zenek Oct 26 '20 at 18:08
  • You may want to visit [this link](https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function) for a comparison of file opening modes - when you're opening it for the second time in "w" mode it gets overwritten with the single line that you're writing to it. Use "a" to append. Apart from this, your calls to `.close()` are missing parentheses. Then, the logic for checking if it's "Already in List!" seems totally out of place. Maybe you should turn `list_of_lists` into a list of tuples, then do `if tuple(name) in list_of_tuples: ...`? – Czaporka Oct 26 '20 at 18:43
  • If you are using an IDE **now** is a good time to learn its debugging features - like setting breakpoints and examining values. Or you could spend a little time and get familiar with the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Also, printing *stuff* at strategic points in your program can help you trace what is or isn't happening. – wwii Oct 26 '20 at 18:57

1 Answers1

1

I think what you want to do is the following:

# Start Downloading System
name = input(Fore.WHITE + "Enter Profile Name To Download: ")

# Save Account (used for check the updates!)
file_object = open("account.zk", "r")

list_of_lists = []
for line in file_object:
  stripped_line = line.strip()
  line_list = stripped_line.split()
  list_of_lists.extend(line_list)  # add the elements of the line to the list

if name in list_of_lists:  # check if the name is already in the file
    print("\n\nAlready in List!\n\n")
    file_object.close()
    time.sleep(5)
else:  # if not, add it
    file_object.close()
    file_object = open("account.zk", "w")
    file_object.write(name + "\n")
    file_object.close()
vvvvv
  • 25,404
  • 19
  • 49
  • 81