Alex
Alex

Reputation: 15

Adding names to a python list

Hi I'm trying to work on this problem. I know my code is wrong but I don't know how to fix it. If anyone could help, that would be great, thank you so much!

Write and test a function that asks for the names of all the members in a club. However, we don't know how many members are actually in the club. Use a “while-loop” which will simply repeat until all the member's names have been entered. How will the “while-loop” know all the member's names have been entered?

def club():
  members = []
  done = False
  while (not done):
    mem = input("enter name")
    if name == "done":
      return False
    else:
      return (members.append(mem)

 File "main.py", line 10

                                     ^
SyntaxError: unexpected EOF while parsing
 

Upvotes: 0

Views: 5397

Answers (1)

PxlPrfctJay
PxlPrfctJay

Reputation: 21

You're specifying an undefined variable "name" to equal "done". I'm assuming you want the user to enter "done" to signify that all names have been entered. Change the if statement to read what is shown below.

You also have the if statement returning False. Which will end up giving you an infinite loop. Make done equal to True to exit the loop.

def club():
  members = []
  done = False
  while done != True:
    mem = input("Enter a name, enter 'done' when finished: ")
    if mem == "done":
      done = True 
    else:
      members.append(mem)
    print(members)
club()

Here is the solution that works for what you're looking at. I added print(members) at the end to show you how the list of members grows until 'done' is typed. Check out the output below.

  1. Input: Enter a name, enter 'done' when finished: James
  2. Output: ['James']
  3. Input: Enter a name, enter done when finished: Dawn
  4. Output: ['James', 'Dawn']
  5. Input: Enter a name, enter done when finished: Jim
  6. Output: ['James', 'Dawn', 'Jim']
  7. Input: Enter a name, enter done when finished: done

Upvotes: 1

Related Questions