-2

Doing an assignment, this is my first program so bear with me. I cant get the while loop to end although i have broke it. I need a way to get out of the loop, and what I'm doing isn't working. Any suggestions would be very helpful thank you.

def main(): #Calls the main function
    while True:
        try:
            name = input("Please enter the student's name: ") #Asks for students name
            while name == "":
                print("This is invalid, please try again")
                name = input("Please enter the students name: ")


        teacher_name = input("Please enter the teacher's name: ") #Asks for teachers name 
        while teacher_name == "":
            print("This is invalid, please try again")
            teacher_name = input("Please enter the teacher's name: ")


        marker_name = input("Please enter the marker's name: ") #Asks for markers name
        while marker_name == "":
            print("This is invalid, please try again")
            marker_name = input("Please enter the marker's name: ")
            break



    except ValueError:
        print("This is invalid, please try again")
4
  • Some more information would be helpful, you have multiple while loops? Which are you looking to break from.
    – Chris
    Mar 29, 2018 at 1:34
  • i am looking to break after asking for markers name. thank you Mar 29, 2018 at 1:35
  • So you would like to break out of both while loops? You could use return
    – Chris
    Mar 29, 2018 at 1:36
  • yeah, ive tried putting return instead of break but it still doesnt break out of the while loop Mar 29, 2018 at 1:38

3 Answers 3

2

The problem with your code is your indentation. You have told the program to break when the marker_name is an empty string. I am assuming that you would like the code to finish when all three values are correct, so as such the following code should work for you:

def main():
    while True:
        try:
            name = input("Please enter the student's name: ") #Asks for students name
            while name == "":
                print("This is invalid, please try again")
                name = input("Please enter the students name: ")


            teacher_name = input("Please enter the teacher's name: ") #Asks for teachers name 
            while teacher_name == "":
                print("This is invalid, please try again")
                teacher_name = input("Please enter the teacher's name: ")


            marker_name = input("Please enter the marker's name: ") #Asks for markers name
            while marker_name == "":
                print("This is invalid, please try again")
                marker_name = input("Please enter the marker's name: ")
            break

        except ValueError:
            print("This is invalid, please try again")


main()

I am a little confused why you have used a try and except? What is the purpose of it?

2
  • No worries, if this fixed your problem please click the tick on my answer.
    – Chris
    Mar 29, 2018 at 1:50
  • i for sure did :) Mar 29, 2018 at 1:52
1

May I ask why the code block is wrapped in the try-except?

Some suggestions:

  • remove the try-except as you shouldn't be raising any errors
  • remove the break statement (after marker_name) as the loop should end when the input is valid
  • ensure the indentation of all the input while-loop code blocks are identical (your formatting is messed up so I'm not sure if you have nested while loops)

Let me know how this works

0
1

Well first of all you break out of a while loop in python with break as you did already. You should only break if a condition you set is met in the loop. So lets say you wanted to break in a while loop that counts and you want to break if the number reaches 100, however, you already had a condition for your while loop. You would then put this inside your while loop.

if x == 100:
    break

As you have it now you just break in your while loop after a couple lines of code without a condition. You will only go through the loop once and then break every single time. It defeats the purpose of a while loop.

What exactly are you trying to do in this code? Can you give more detail in your question besides that you would like to break in a while loop? Maybe I can help you more than giving you this generic answer about breaking in a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.