[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-08-19
11
Medium Priority
?
225 Views
Last Modified: 2014-08-20
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.
0
Comment
Question by:Barbara69
  • 6
  • 5
11 Comments
 
LVL 20

Expert Comment

by:Mark Brady
ID: 40271661
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
 

Author Comment

by:Barbara69
ID: 40273589
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
 
LVL 20

Expert Comment

by:Mark Brady
ID: 40273597
Ahhh sorry yes you should add attempt += 1 on the line below while attempts < 3:
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:Barbara69
ID: 40273787
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
 
LVL 20

Accepted Solution

by:
Mark Brady earned 2000 total points
ID: 40274494
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
 
LVL 20

Expert Comment

by:Mark Brady
ID: 40274502
Line 18 and 19 might need tabbing properly - they didn't paste correctly
0
 

Author Comment

by:Barbara69
ID: 40275173
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
 
LVL 20

Expert Comment

by:Mark Brady
ID: 40275211
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
 

Author Comment

by:Barbara69
ID: 40275223
Please ignore my last comment. I got it to work.
0
 

Author Closing Comment

by:Barbara69
ID: 40275227
Thanks for all of your assistance Mark.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 40275278
You're most welcome
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question