I have been trying to create the following program in Python. However, I am getting frustrated with my program, and I have no idea what I should be doing! Specifically, I am having trouble appending elements to different lists based on my if-else statements. What is supposed to happen is the userInputMembers should get assigned to an array depending on the number of new members inputted by the user, so the program outputs how many new members are assigned to each array.
Instead, I just end up with empty arrays. I am still learning how Python works, so right now I am just really confused!
Here is what the final output of my program looks like:
Number of Trainers: []
0 trainers have 0-5 new enrollees
0 trainers have 6-10 new enrollees
0 trainers have 11-15 new enrollees
Below is the code for my current program:
#Declare variables:
NUM_MEMBERS = 15
trainerNames = []
newMembers = []
newMembers1 = []
newMembers2 = []
newMembers3 = []
#Intitialize loop:
for i in range(NUM_MEMBERS):
userInputTrainers = input("Enter a trainer's last name")
userInputMembers = input("Enter the number of new members enrolled")
for userInputMembers in newMembers:
a = userInputMembers[1]
if a <= 5:
newMembers1.append(userInputMembers)
elif 5 < a <= 10:
newMembers2.append(userInputMembers)
elif 10 < a <= 15:
newMembers3.append(userInputMembers)
else:
print("Sorry, there isn't a category for that.")
userInputTrainers = trainerNames
trainerNames = len(trainerNames)
print("Number of Trainers:",trainerNames)
print(len(newMembers1), "trainers have 0-5 new enrollees.")
print(len(newMembers2), "trainers have 6-10 new enrollees")
print(len(newMembers3), "trainers have 11-15 new enrollees")
*edit: I realize that my post is a duplicate of other people's post who have attempted the same assignment in python. I found someone else's code that was "fixed" (included below)
- Apologies to the person's original post whom I took the following code from. I have attempted to recognize who they are, but I lost their post in the process.
Here is an answer I have found to my problem above from another post:
#Declare variables:
newMembers1 = []
newMembers2 = []
newMembers3 = []
newMembers = []
for i in range(1, 15):
t = input(f"Enter Trainer {i} Name: ")
n = int(input(f"Enter Trainer {i} # of enrollees: "))
L = [t, n]
newMembers.append(L)
I realize that their code works better than mine because of how they assigned each input to a different variable, and appended it to data.append as shown above. Based on their code, I have attempted to fix my code as shown below:
#Declare variables:
newMembers1 = []
newMembers2 = []
newMembers3 = []
newMembers = []
for i in range(1, 15):
t = input(f"Enter Trainer {i} Name: ")
n = int(input(f"Enter Trainer {i} # of enrollees: "))
L = [t, n]
newMembers.append(L)
for trainers in newMembers:
a = trainers[1]
if a <= 5:
if type(n) == int:
newMembers1.append(trainers)
elif 5 < a <= 10:
if type(n) == int:
newMembers2.append(trainers)
elif 10 < a <= 15:
if type(n) == int:
newMembers3.append(trainers)
else:
print("Sorry, there isn't a category for that.")
print(len(newMembers1), "trainers have 0-5 new enrollees.")
print(len(newMembers2), "trainers have 6-10 new enrollees")
print(len(newMembers3), "trainers have 11-15 new enrollees")
if userInputMembers == category0_5
doesn't work, that will compare a single string with arange
object. You needif int(userInputMembers) in category0_5
.category0_5 == range(0,5)
is comparison not assignment. Please actually try running your code, as that would have lead to an error.array
in your code. What you have is alist
.