I have a names and numbers in my file (name of the file 'phone_book') and I tried to read all data using this code:
def read_phonebook():
read = open("phone_book.txt", 'r')
i = 0
for i in (len(read)):
print(i + '\t')
read.close()
while True:
if menu == 2:
read_phonebook()
but it gives Error: read_phonebook file has no len()
If I don't use len it keeps printing data because I'm using While loop. Could someone explain me how can I make this function to read list's whole data? with using While Loop?
menu? Why do you seti = 0if the next command overwrites it? Why are you expectinglento give you an iterable? It would help you to make a minimal reproducible example.for i in range(len(read)):... Butreadhere is an iterable object, so usinglen()would actually iterate the entire thing before you printed each line, and you're left with a fully consumed iterable, meaning nothing can be printed after