Reputation: 27
I cant stop it from going back to rosterInput even when 0 is entered. (its in the right format in the ide but I cant seem to make it right on here for some reason.) Also will this properly store multiple inputs from the user in a list that I can later print in a different function?
def add():
#adding team members...
global roster
roster = []
rosterInput = roster.append(input("New member or members: "))
while rosterInput != "0":
add()
if rosterInput == "0":
mainMenu()
Upvotes: 0
Views: 27
Reputation: 8744
Your problem is that you're saving the result of roster.append
into rosterInput
, not the result of input
. Append returns None
.
Instead, do
rosterInput = input("New member or members: ")
roster.append(rosterInput)
Edit: To correct the issue of never going to the main menu, move the if clause outside of the while clause, and you don't need the if anyway. Also, your rosterInput
never changes during the while
-loop, so I think you should read rosterInput
again after add()
. In summary, to do it iteratively:
rosterInput = input("New member or members: ")
while rosterInput != "0":
roster.append(rosterInput)
rosterInput = input("New member or members: ")
mainMenu()
To do it recursively, which is maybe closer to your original formulation:
roster = []
def add():
# adding team members...
rosterInput = input("New member or members: ")
if rosterInput != "0":
roster.append(rosterInput)
add()
else:
mainMenu()
Initialize roster with the empty list outside the function to not overwrite it each time. You don't need the global
statement here, since you're not reassigning roster, just mutating with append
.
Upvotes: 1