0

I need to write a code in Python using the while loop to do the following:

  1. Require the user to enter the names of all pupils in a class.
  2. The user should be able to type “Stop” to indicate that the names of all the students have been entered.
  3. Print out the total number of names the users entered after the loop has been exited.

Here is the is the code I thus far:

print ("""Enter the names of all the pupils in your class.
Type 'Stop' to indicate that all the names have been entered.""")

count_names = {}

while True:
    pupil_names = input("Enter a name: ")
    # I know there is code missing here, but I can not figure out what it must be
    # I have 'count_names' as my counter variable

    if pupil_names == "Stop":
            print(f"There are {count_names} students")
        
            break

I have searched everywhere for help but can't find anything.
Answers / Help that have come the closest:
count number of names in list in python
Counting the input in a while loop?

René Nel
  • 31
  • 9
  • `count_names` should be a number, not a dictionary. Then you just add 1 to it each time through the loop. – Barmar Jan 02 '21 at 23:22
  • The two questions you linked to are about counting repetitions of the same name, not for counting the total number of names entered. – Barmar Jan 02 '21 at 23:23
  • @Barmar Thank you, and yes I know. But I just added them to show research and that there where no other answers to my question. – René Nel Jan 02 '21 at 23:51

4 Answers4

1

Try the following code

print ("""Enter the names of all the pupils in your class.
Type 'Stop' to indicate that all the names have been entered.""")

count_names = 0 # Initialize count_names to 0

while True:
    pupil_names = input("Enter a name: ")
    
    if pupil_names == "Stop":
            print(f"There are {count_names} students")
            break

    count_names += 1 # Add 1 each time we count a pupil
mandulaj
  • 733
  • 3
  • 10
1

By setting count_names to {} you make it a dictionary which does not suit your purpose. You would either want to save all names to a list ([]) or just keep the number of names as a number (start with 0).

names_list = [] # version 1
names_count = 0 # version 2

while True:
    new_name = input("Enter a name: ")

    if new_name == "Stop":        
        break

    names_list.append(new_name) # version 1
    names_count += 1            # version 2

print(f"There are {len(names_list)} students") # version 1
print(f"There are {names_count} students")     # version 2

Also, I put the print statement outside of the while-block, as your task asked for it. It will run exactly the same.

PieCot
  • 3,564
  • 1
  • 12
  • 20
Dames
  • 776
  • 3
  • 11
0

change count_names to:

count_names = 0

and add the following line:

while True:
  pupil_names = input("Enter a name: ")
  count_names += 1

This should work

Tobias
  • 33
  • 7
0

I hope this helps as this is my take on this problem:

print('''Enter the names of all the pupils in 
the class. Enter "Stop" once all the names have 
been entered.
''')

pupil_count = 0

while pupil_count >= 0:
   pupil_names = input("Enter a name: ")

   pupil_count += 1

   if pupil_names == "Stop":

       print(f"\nThe total number of pupils in the class is: {pupil_count - 1}")

       break