0

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
 
2
  • "How will the “while-loop” know all the member's names have been entered?" - According to your code, name "done" is an indicator of the end of the names' list. But for some reason, you read the user's input into mem variable, but check the name one. As for SyntaxError: error, without the complete code (and pointing to line 10) we cannot help you. Commented Jun 10, 2019 at 22:28
  • return (members.append(mem) is missing a closing parenthesis ')'. This is causing the syntax error. However, you do not need parentheses around the return value, or '(not done)'. Commented Jun 10, 2019 at 22:40

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could also just do while True, use a break in the if, and input before and in the loop (known as priming the loop) but this solution is just fine as well (just uses an extra variable..
Agreed. Figured it'd help illustrate when their question of "when does the while loop know when all of the names have been entered"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.