0

Below is what I'm trying to do:

  1. Prompt user with the question "Do you want to add a student? "
  2. If user enters "Yes" they will be prompted with "Who do you want to add? "
  3. After adding the student's name, they will be prompted again with "Do you want to add a student? " until the user enters "No"
  4. If the user enters "No", I want to print all the students the user has entered.

With the little I know, I attempt to code it below:

students = []


initial_input = 'Yes'
second_input = ''

while initial_input == 'Yes':
    initial_input = input('Do you want to add a student? ')
    if initial_input == 'Yes':
        second_input = input('Who do you want to add? ')

        if initial_input == 'No':
            students.append(second_input)

print(students)

When running it, it doesn't print out the names I entered below:

Do you want to add a user? Yes
Who do you want to add? John
Do you want to add a user? Yes
Who do you want to add? Tim
Do you want to add a user? No
[]

Process finished with exit code 0

Can someone kindly explain what I'm doing wrong?

Nina G
  • 269
  • 5
  • 17

1 Answers1

1

It's a simple placement issue. Your appending of the student only comes if they put 'no' to adding a student. Try this:

while initial_input == 'Yes':
    initial_input = input('Do you want to add a student? ')
    if initial_input == 'Yes':
        second_input = input('Who do you want to add? ')
        students.append(second_input)

print(students)
Craig Brett
  • 2,295
  • 1
  • 22
  • 27