0

I encounter the following exercise. 1. Write a function named readposint that keeps prompting the user for a positive integer, checks the input to confirm that it meets the requirements. It should be able to handle inputs that cannot be converted to int, as well as negative ints, and edge cases (e.g. when the user closes the dialog, or does not enter anything at all.)

So, my question is: how to handle the edge case that when the user closes the dialog? I feel like this exercise is to keep asking the user to input a positive number, and the user simply can not leave the dialog without inputing a correctly positive number. Could anyone help me with this please? Many thanks for your time and attention.

highsky
  • 125
  • 3
  • 11
  • https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement, https://docs.python.org/3/tutorial/errors.html#raising-exceptions – wwii Dec 13 '16 at 16:33
  • You should try to craft a solution, most *dialogs* will return something based on the user's action, you can raise an exception based on the return *value*. – wwii Dec 13 '16 at 16:40
  • 1
    What do you mean by "close the dialog"? – abukaj Dec 13 '16 at 16:52
  • @wwii What do you mean by "craft a solution"? – highsky Dec 13 '16 at 16:59
  • @abukaj, yeah. That's the point. I actually got the exercise from the book "how to think like a computer scientist" book. The original question is: Write a function named readposint that uses the input dialog to prompt the user for a positive integer and then checks the input to confirm that it meets the requirements. It should be able to handle inputs that cannot be converted to int, as well as negative ints, and edge cases (e.g. when the user closes the dialog, or does not enter anything at all.) – highsky Dec 13 '16 at 17:00
  • Sorry, I meant you should try to figure it out and write a solution. If you cannot get it to work - come back and ask specific questions about a problem you are having. – wwii Dec 13 '16 at 17:04
  • The exercise there is very confusing. So I transfer it to my own version. I am running it using my IDLE. So, I think, "close the dialog" means when you close the python shell window. – highsky Dec 13 '16 at 17:05
  • @wwii. Yeah. the thing is, I do not know how to handle the exception of "close the dialog". – highsky Dec 13 '16 at 17:08
  • 1
    Read the question again, what type of dialog are you talking about? Take a look at the possible duplicate link in my first comment. – wwii Dec 13 '16 at 17:10
  • 1
    @highsky when you close the python shell window, you end the program execution so handling the exception is no longer an issue. Neither closing the dialog nor not entering anything is an option when you use `input()` built-in function (https://docs.python.org/3.5/library/functions.html#input; I consider both new line character and end of file character to be something) – abukaj Dec 14 '16 at 09:52
  • @wwii, I do not think this question is properly answered in the duplicate link. The thing confusing here is, how to raise an exception when the user is trying to close the dialog of prompting the input. I know how to ask the user for input until they give a valid response. I just do not know how to avoid the user doing something like close the dialog and leave. – highsky Dec 14 '16 at 13:51
  • What type of *dialog* are you talking about - that excercise specifies using ```input()```, how would a user close it? – wwii Dec 14 '16 at 15:30
  • @wwii, thank you so much. I think this exercise is just too confusing. The expression of itself just does not make sense. – highsky Dec 14 '16 at 22:04

0 Answers0