How to print an error message and exit after a while loop ends?

I have a program in python 3 with a global variable for my counter, while counter <= 3, gets the user input, goes to a function to validate and increments the counter in the validation function. After three invalid data entries in a row, it needs to display an error message and exit. It exits the while loop after the three invalid attempts in a row, but I don't know where to put the error message and how to make it exit. If the user enters invalid data, say twice and the third time enters good data how to reset the counter and start the process all over again.
Barbara69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BradyPrincipal Data EngineerCommented:
Here is a small example and I am assuming it is inside a class. If not jusr remove all of the self. before my code

self.attempts = 0
self.answered_correctly = None

while attempts < 3:
    result = your_function_name() # this is where you would call your function/method or you raw input call
   
    # call your method to validate here - or just evaluate the result directly
    if result is not None:
        # set some variable here
        self.answered_correctly = True
        break # stop the loop here

# now did we get the correct answer?
if not self.answered_correctly:
    # print your error message here
    print 'Bad answer. Now you must PAY!'

    # and if you want your script to die right there -  add this to the top of your file:  import sys

    # then type here:
    sys.exit()

# if you get here then they got the answer correct!


Hope this helps!
0
Barbara69Author Commented:
I think I understand your code, but you have while attempts < 3, but nowhere in your code does attempts get incremented or reset or did I miss something?
0
Mark BradyPrincipal Data EngineerCommented:
Ahhh sorry yes you should add attempt += 1 on the line below while attempts < 3:
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Barbara69Author Commented:
The program should display the error message after 3 invalid attempts in a row and exit. What happens if the user enters 2 invalid attempts and on the 3 attempt enters valid data than how does the counter get reset and the process start all over again.
0
Mark BradyPrincipal Data EngineerCommented:
ok try this.
self.attempts = 0
self.answered_correctly = None

while not self.answered_correctly:
    if self.attempts == 3:
        print 'You answered incorrectly 3 times!'
        sys.exit() # die here or do something else

    # call your function - it should return None if the answer is incorrect else return True
    result = your_function_name() # this is where you would call your function/method or you raw input call
    
    # call your method to validate here - or just evaluate the result directly
    if result is not None:
        # set some variable here
        self.answered_correctly = True
        self.attempts = 0 # reset it here
        break # stop the loop here
   else:
       self.attempts += 1

# if they get here the answer is correct and self.attempts will be 0

Open in new window


Of course I am assuming this is inside a class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BradyPrincipal Data EngineerCommented:
Line 18 and 19 might need tabbing properly - they didn't paste correctly
0
Barbara69Author Commented:
Where you have
# call your function - it should return None if the answer is incorrect else return True
    result = your_function_name() # this is where you would call your function/method or you raw input call
I have 6 lines of data to get from the user, so how would the above work?
0
Mark BradyPrincipal Data EngineerCommented:
Well I am working blind here. I don't know what type of data or how much you are getting. Explain in detail what you are trying to do. Do you already have a function to parse the results? What I am trying to do is point you in the right direction and not just write the entire code for you. This is how we all learn. But go ahead and explain exactly what you need to do and what are the conditions.
0
Barbara69Author Commented:
Please ignore my last comment. I got it to work.
0
Barbara69Author Commented:
Thanks for all of your assistance Mark.
0
Mark BradyPrincipal Data EngineerCommented:
You're most welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.