Intermediate Python3 - Generators - Event Coordinator Project

Hello, Trying to work my way through intermediate python3 and seem to be struggling on the generators project. Also this is my first time posting so any advice on how to improve posting a new thread is appreciated.

this is the link to the project. I haven’t even gotten through step one.

Event Coordinator Project link

Project Files

script.py

guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  while True:
    line_data = text_file.readline().strip().split(",")
    if len(line_data) < 2:
    # If no more lines, close file
      text_file.close()
      break
    name = line_data[0]
    age = int(line_data[1])
    guests[name] = age
    #Task 1
    yield guests
  
#Task 1
guest_list_generator = read_guestlist(file_name)
for guest in guest_list_generator:
  print(guest)
    
#Traceback (most recent call last):
  #File "script.py", line 17, in <module>
    #guest_list_iter = read_guestlist(file_name)
#NameError: name 'file_name' is not defined

guest_list.py

Tim,22
Tonya,45
Mary,12
Ann,32
Beth,20
Sam,5
Manny,76
Kenton,15
Kenny,27
Dixie,46
Mallory,32
Julian,4
Edward,71
Rose,65

I know it something simple, but the instructions seem to be pretty ambiguous in the more advanced classes and there doesn’t seem to be any cheatsheets or walkthrough videos for this course yet.

my personal attempts are under the comments: # Task 1

Check the argument… Should it be something other than, fille_name?

Hey mtf thanks for the reply.

I attempted:

guest_list_generator = read_guestlist(guests)

for guest in guest_list_generator:

  print(guest)

returned:

#Traceback (most recent call last):
  #File "script.py", line 18, in <module>
    #for guest in guest_list_generator:
  #File "script.py", line 3, in read_guestlist
    #text_file = open(file_name,'r')
#TypeError: expected str, bytes or os.PathLike object, not dict

I also attempted:

#Task 1

guest_list_generator = read_guestlist(guest_list)

for guest in guest_list_generator:

  print(guest)

returned:

#Traceback (most recent call last):
  #File "script.py", line 17, in <module>
    #guest_list_generator = read_guestlist(guest_list)
#NameError: name 'guest_list' is not defined

Do I need to import the guest_list.txt before beginning the project? Or am I not putting in the correct input when calling the generator function?

No, it cannot be imported. Just use the file name in quotes. 'guest_list.txt'.

that was it thank you! I didnt realize I had to put it in ’ ’

Hey mtf I have another question about generators.

task 3 of the same project is asking us to add numerous next() calls on the generator object until a StopIteration exception is reached. it says I can also use a for loop.

I added a next() in my line of code and already have an error coming back. Is there an error in the way I implemented the send() for task 2 or am I not formatting my generator correctly? I have really struggled with this lesson unfortunately.

my current script.py


guests = {}
def read_guestlist(file_name):
  text_file = open(file_name,'r')
  #text_file.send('Jane,35')
  while True:
    line_data = text_file.readline().strip().split(",")
    if len(line_data) < 2:
    # If no more lines, close file
      text_file.close()
      break
    name = line_data[0]
    age = int(line_data[1])
    guests[name] = age
    #Task 1
  yield guests

#Task 1
guest_list_generator = read_guestlist('guest_list.txt')
for guest in guest_list_generator:
  #task 2
  if guest is None:
    guest_list_generator.send('Jane,35')
  print(guest)

#Task 3
next(guest_list_generator)

console is returning:


{'Tim': 22, 'Tonya': 45, 'Mary': 12, 'Ann': 32, 'Beth': 20, 'Sam': 5, 'Manny': 76, 'Kenton': 15, 'Kenny': 27, 'Dixie': 46, 'Mallory': 32, 'Julian': 4, 'Edward': 71, 'Rose': 65}
Traceback (most recent call last):
  File "script.py", line 23, in <module>
    next(guest_list_generator)
StopIteration

TBH, I’ve not got to this unit yet and will a day or two to get up to speed.

I think the issue is that when you yield the guests dict, you yield the entire dict after the while loop has been completed. To complete the first step, I yielded the line_data inside the loop, and it looked like this:

I hope I helped!

1 Like