Python Forum

Full Version: while loop stumbling block
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys, I am very new to coding so please bare with me. I am busy with a python bootcamp trying to learn as much as quick as I can. I am currently busy with loops, while loops in particular. What I need to make the program do:

ask a user via input command to enter all student names and once he enters "Stop" the loop of asking him/her to enter the next name must stop. I think I have that part sorted. The next part is my struggle:

After the user enters: "Stop" :

The names of the students needs to be printed out, without the "Stop" being printed as that is obviously not a students name.


My attempt at coding the above looks like this:

names = input("Enter student name: ")
while names != "Stop":
    names = input("Enter student name: ")
    if names == "Stop":
        print(names)
So the result I am getting is the phrase "Stop" being printed instead of the student names entered prior to "Stop"

I would deeply appreciate some assistance.
Thanks in advance.

The YoungGrassHopper
You need two variables: one for the name most recently entered, and one for the list of names that have been entered. Call them next_name and names. The input statement goes to next_name. If that's 'stop', you break out of the loop and print names. Otherwise, you append next_name to names.
Thanks for the prompt response ichabod801 I really appreciate it stacks.
(Sep-09-2019, 06:35 PM)ichabod801 Wrote: [ -> ]You need two variables: one for the name most recently entered, and one for the list of names that have been entered. Call them next_name and names. The input statement goes to next_name. If that's 'stop', you break out of the loop and print names. Otherwise, you append next_name to names.

Ok,

I gave it a try, but what I am getting is only the first name printed and not more than one name.
The user will obviously enter 1 name, press enter, get requested to enter another name... until he enters "Stop" I only seem to get the first name entered..
I literally started coding past Thursday so please don't get angry with me.
It would be best to explain as if I am a 4 year old. And I apologise if I am proving to be a waste of your precious time.

next_name = input("Enter student name: ")
names = 0
while names != "Stop":
    names = input("Enter student name: ")
    if names == "Stop":
        print(next_name)
    

Another while loop stumbling block I have is this:

The program needs to ask the user to repeatedly enter a number and stop + exit the loop once the user enters:
"-1"
The program must then calculate the average of the numbers entered  excluding the -1. 


The first part I think I have correct, but I do not know how to calculate the average of all the inputted numbers prior to " -1 "


Here is what I have so far:


num = int(input("Please enter a number: "))
while num != -1:
    num = int(input("Please enter a number: "))

    
You are keeping your variables separate enough. This is an outline of what your code should look like for both problems:

data = []
while True:
    next_datum = input('What is the next whatever? ')
    if next_datum == stop_value:
        break
    data.append(next_datum)
print(data)
next_datum is what they just entered. data is a list of everything valid that they entered. stop_value is whatever response stops the loop.

For the one with numbers, you want to look at the int() function to convert strings to integers.
(Sep-09-2019, 08:25 PM)ichabod801 Wrote: [ -> ]You are keeping your variables separate enough. This is an outline of what your code should look like for both problems:

data = []
while True:
    next_datum = input('What is the next whatever? ')
    if next_datum == stop_value:
        break
    data.append(next_datum)
print(data)
next_datum is what they just entered. data is a list of everything valid that they entered. stop_value is whatever response stops the loop.

For the one with numbers, you want to look at the int() function to convert strings to integers.

Ok, I see, I will try and recode it following your suggested code outline, thanks again so much for your help I do appreciate it :)