0

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")
4
  • 1
    if userInputMembers == category0_5 doesn't work, that will compare a single string with a range object. You need if int(userInputMembers) in category0_5. Commented Oct 17, 2023 at 0:38
  • 1
    Oh and not to mention that category0_5 == range(0,5) is comparison not assignment. Please actually try running your code, as that would have lead to an error. Commented Oct 17, 2023 at 0:40
  • 1
    Or maybe that error is what you're asking about? Please take some time to read How to Ask, as well as about how to write the "perfect" question, especially its checklist. Commented Oct 17, 2023 at 0:42
  • You talk about "arrays" in your question, but there is no array in your code. What you have is a list.
    – Matthias
    Commented Oct 17, 2023 at 7:33

1 Answer 1

1

Lots wrong here.

First, you never actually add anything to newMembers. It starts out as an empty list, and it stays that way. How were you expecting anything to get added to it?

Second, these three lines don't actually do anything, because you're using == which is a comparison, not an assignment:

    category0_5 == range(0,5)
    category6_10 == range(6,10)
    category11_15 == range(11,15)

Third, this comparison will never be true, because input() returns a string. You need to convert the input to an integer.

if userInputMembers == category0_5:

And even if you did convert to an integer, it still wouldn't be true, because an integer is not equal to a range. Did you mean to check if the range contains the integer? Use in instead of ==:

if userInputMembers in category0_5:

And even then, it still might not be true, because range() does not include the final number. So range(0,5) includes 0,1,2,3,4. It does not include 5.

11
  • Hello! Thanks for your help! I realize that range may not be the best command, I should probably have used comparisons like if userInputMembers <= 5, or something like that. I found someone else's post with the same problem that I have, and I am trying to look at their code. Unfortunately, I think I lost their post, but I will try to edit my code above using their comparisons.
    – Selah
    Commented Oct 17, 2023 at 1:43
  • @Selah You can use an expression like this if 6 <= x <= 10: Commented Oct 17, 2023 at 1:45
  • Great! does my code look better now?
    – Selah
    Commented Oct 17, 2023 at 2:03
  • @Selah Run it, and see if it does what you want. Commented Oct 17, 2023 at 2:47
  • It is no longer displaying any of my print statements.
    – Selah
    Commented Oct 17, 2023 at 14:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.